Enjoying This Tutorial?
Subscribe to Ram N Java for high-quality, practical Java and Spring tutorials!
SUBSCRIBE NOWIntroduction
In Spring Framework applications, assembling collaborating objects is at the heart of Inversion of Control (IoC). When an object depends on another custom object—such as an Employee possessing an Address—Spring allows you to wire this dependency cleanly using the <property> element along with the ref attribute via setter-based dependency injection.
Why Inject Dependent Objects via Setter Method?
- Loose Coupling: Eliminates direct instantiation (e.g., calling
new Address()) within the dependent class, making classes modular and easily swappable. - Declarative Wiring with
ref: Therefattribute points to the ID of another bean defined in the container, wiring complex graphs of objects effortlessly. - Flexibility: Setter injection makes dependencies optional and allows them to be reconfigured or mocked easily during unit testing.
Step-by-Step Code Example
Here is an example demonstrating an Employee class receiving a dependent Address object via setter injection:
1. The Dependent and Target Model Classes
package com.ram.model;
// Dependent Object
public class Address {
private String city;
private String state;
public void setCity(String city) {
this.city = city;
}
public void setState(String state) {
this.state = state;
}
@Override
public String toString() {
return "Address [city=" + city + ", state=" + state + "]";
}
}
// Target Class holding the Dependent Object
public class Employee {
private String name;
private Address address;
public void setName(String name) {
this.name = name;
}
// Setter method for dependent object injection
public void setAddress(Address address) {
this.address = address;
}
public void display() {
System.out.println("Employee Name: " + name);
System.out.println("Address: " + address);
}
}
2. Configuring applicationContext.xml
Define the collaborator bean first, then reference it using the ref attribute inside the target bean's <property> tag:
<!-- Dependent Address Bean -->
<bean id="addressBean" class="com.ram.model.Address">
<property name="city" value="Bangalore" />
<property name="state" value="Karnataka" />
</bean>
<!-- Employee Bean with Dependent Object Setter Injection -->
<bean id="employee" class="com.ram.model.Employee">
<property name="name" value="Ram" />
<property name="address" ref="addressBean" />
</bean>
3. Loading and Executing with ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = (Employee) context.getBean("employee");
emp.display();
Key Takeaway
Using the ref attribute inside a <property> element allows Spring's IoC container to resolve and wire dependent collaborator objects into beans via their setter methods, maintaining a cleanly decoupled architecture.
Recommended Dependency Injection Tutorials from Ram N Java
Click here to watch on Youtube:
https://www.youtube.com/watch?v=NaftatBNCwM&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
| How to Inject dependent Object using Spring dependency injection by Setter method? |
| How to Inject dependent Object using Spring dependency injection by Setter method? |
| How to Inject dependent Object using Spring dependency injection by Setter method? |
| How to Inject dependent Object using Spring dependency injection by Setter method? |