Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
Dependency Injection (DI) is a core concept in the Spring framework. It is the mechanism by which Spring provides the dependencies of a bean. There are several ways to achieve DI in Spring: via constructor injection, setter method injection, and field injection.
Here, we'll focus on Setter Method Injection.
In setter method injection, the container will inject the dependencies through the bean's setter methods. For the setter method injection to work, you'll:
Consider we have a MessageService
and a MessagePrinter
class:
public class MessageService { private String message; public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } } public class MessagePrinter { private MessageService messageService; // Setter method for DI public void setMessageService(MessageService messageService) { this.messageService = messageService; } public void printMessage() { System.out.println("Message: " + messageService.getMessage()); } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="messageService" class="com.example.MessageService"> <property name="message" value="Hello, Spring!"/> </bean> <bean id="messagePrinter" class="com.example.MessagePrinter"> <property name="messageService" ref="messageService"/> </bean> </beans>
In the above configuration:
<property>
tag corresponds to the setter methods of the bean class.name
attribute of <property>
should match the name of the property (field) in the bean.ref
attribute refers to another bean definition (in this case, messageService
).public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); MessagePrinter printer = context.getBean("messagePrinter", MessagePrinter.class); printer.printMessage(); } }
When you run the application, Spring initializes the beans, and the dependencies are injected using the setter methods. The output will be:
Message: Hello, Spring!
Setter injection is especially useful when:
However, note that in modern Spring applications, constructor injection is often favored over setter injection as it ensures the immutability of the bean's dependencies once they're set and promotes cleaner and more concise code.
Setter-based Dependency Injection in Spring:
// UserService.java public class UserService { private UserRepository userRepository; // Setter method for userRepository public void setUserRepository(UserRepository userRepository) { this.userRepository = userRepository; } // Other methods... }
How to use setter injection in Spring framework:
// OrderService.java public class OrderService { private OrderRepository orderRepository; // Setter method for orderRepository public void setOrderRepository(OrderRepository orderRepository) { this.orderRepository = orderRepository; } // Other methods... }
Injecting dependencies using setter methods in Spring:
@Autowired
to enable Spring to inject the dependencies.// ProductService.java public class ProductService { private ProductRepository productRepository; @Autowired public void setProductRepository(ProductRepository productRepository) { this.productRepository = productRepository; } // Other methods... }
Configuring dependencies with setter methods in Spring:
<!-- applicationContext.xml --> <bean id="userService" class="com.example.UserService"> <property name="userRepository" ref="userRepository" /> </bean>
Using @Autowired with setter methods in Spring:
@Autowired
for automatic dependency injection.// UserService.java public class UserService { private UserRepository userRepository; @Autowired public void setUserRepository(UserRepository userRepository) { this.userRepository = userRepository; } // Other methods... }