🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, we will learn how to use field-based dependency injection in the Spring boot application.
Dependency Injection is a design pattern on which dependency of the object is injected by the framework rather than created by the Object itself - It is also called IOC (Inversion of Control).
Dependency Injection reduces coupling between multiple objects as its dynamically injected by the framework. Spring IoC Container uses DI to inject one object into another object.
There are mainly three types of Dependency Injection:
Field Injection
Field Injection Example
MessageService
public interface MessageService {
void sendMessage(String message);
}EmailService
import org.springframework.stereotype.Component;
@Component
public class EmailService implements MessageService{
@Override
public void sendMessage(String message){
System.out.println(message);
}
}We have annotated EmailService class with @Component annotation so the Spring container automatically creates a Spring bean and manages its life cycle.
SMSService
import org.springframework.stereotype.Component;
@Component("smsService")
public class SMSService implements MessageService{
@Override
public void sendMessage(String message){
System.out.println(message);
}
}We have annotated SMSService class with @Component annotation so the Spring container automatically creates a Spring bean and manages its life cycle.
MessageSender
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
@Autowired
@Qualifier("emailService")
private MessageService messageService;
@Autowired
private MessageService smsService;
public void sendMessage(String message){
this.messageService.sendMessage(message);
this.smsService.sendMessage(message);
}
}@Qualifier annotation is used in conjunction with Autowired to avoid confusion when we have two or more beans configured for the same type.
Spring @Autowired annotation is used for the automatic injection of beans.
Spring container uses the below fields to inject dependency on any Spring-managed bean (MessageSender is a Spring bean):
@Autowired
@Qualifier("emailService")
private MessageService messageService;
@Autowired
private MessageService smsService;AppConfig
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.spring.core.di")
public class AppConfig {
}Testing
Let's create ApplicationContext and test this example:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Client {
public static void main(String[] args) {
String message = "Hi, good morning have a nice day!.";
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
MessageSender messageSender = applicationContext.getBean(MessageSender.class);
messageSender.sendMessage(message);
}
}Output:
Conclusion
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business

Comments
Post a Comment
Leave Comment