
| Constructor Injection in Spring Boot |
| public class UserService { private UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } } |
| Setter Method Injection In Spring Boot |
| public class UserService { private UserRepository userRepository; public UserService() { } public void setUserRepository(UserRepository userRepository) { this.userRepository = userRepository; } } |
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @service public class MyService { private final MyRepository repository; @Autowired public MyService(MyRepository repository) { this.repository = repository; } public void doSomething() { MyRepository.doSomething(); } |
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MyService { private final MyRepository repository; private final MyLogger logger; @Autowired public MyService(MyRepository repository, MyLogger logger) { this.repository = repository; this.logger = logger; } public void doSomething() { userRepository.doSomething(); orderRepository.placeOrder(); } |
| Setter Method Example In Autowired Spring Boot |
| @Service public class SampleService { private UserRepository userRepository; @Autowired public void setUserRepository(UserRepository userRepository) { this.userRepository = userRepository; } public void doSomething() { userRepository.doSomething(); } } |
| Handling Multiple Dependencies In Setter Method In Spring Boot |
| public class Person { private String name; private int age; private Address address; public Person() { // Default constructor } // Setter method for the name public void setName(String name) { this.name = name; } // Setter method for the age public void setAge(int age) { this.age = age; } // Setter method for the address public void setAddress(Address address) { this.address = address; } public String getName() { return name; } public int getAge() { return age; } public Address getAddress() { return address; } public static void main(String[] args) { Person person = new Person(); person.setName("John Doe"); person.setAge(30); Address address = new Address(); address.setStreet("123 Main St"); address.setCity("Anytown"); address.setZipCode("12345"); person.setAddress(address); System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); System.out.println("Address: " + person.getAddress().getStreet() + ", " + person.getAddress().getCity() + ", " + person.getAddress().getZipCode()); } } class Address { private String street; private String city; private String zipCode; public Address() { // Default constructor } public void setStreet(String street) { this.street = street; } public void setCity(String city) { this.city = city; } public void setZipCode(String zipCode) { this.zipCode = zipCode; } public String getStreet() { return street; } public String getCity() { return city; } public String getZipCode() { return zipCode; } } |
| Suppose we have two beans of the same type “MyService” and we want to inject one of them into another bean “MyController” using @Qualifier annotation. |
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; @Component("service1") public class MyService { public void showMessage() { System.out.println("Service 1 is running."); } } @Component("service2") public class AnotherService { public void showMessage() { System.out.println("Service 2 is running."); } } @Component public class MyController { private final MyService myService; @Autowired public MyController(@Qualifier("service1") MyService myService) { this.myService = myService; } public void displayMessage() { myService.showMessage(); } } |
| @Component("car") @Primary public class Car implements Vehicle { } |