Enjoying this Java & Spring tutorial?
Subscribe to Ram N Java on YouTubeIntroduction
In Spring Framework, Constructor Injection is the industry-standard approach for passing collaborator objects (dependencies) to a target bean. It ensures that a bean cannot be instantiated in an incomplete or invalid state, guaranteeing immutability and facilitating cleaner unit testing.
In this tutorial, you will learn how to inject a custom dependent object into another class using constructor dependency injection through Spring XML configuration.
1. Create the Dependent Class (Address)
Start by defining the dependent collaborator class (for instance, an Address class) with its attributes and constructor:
public class Address {
private String city;
private String state;
private String country;
public Address(String city, String state, String country) {
this.city = city;
this.state = state;
this.country = country;
}
@Override
public String toString() {
return city + ", " + state + ", " + country;
}
}
2. Create the Target Class (Employee)
Create the target class Employee which accepts the dependent Address object along with primitive values through its constructor:
public class Employee {
private int id;
private String name;
private Address address;
public Employee(int id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
public void showEmployeeDetails() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Address: " + address);
}
}
3. Configure Spring XML using <constructor-arg ref="...">
In your applicationContext.xml, declare the Address bean first, then inject it into the Employee bean using the ref attribute inside <constructor-arg>:
<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">
<!-- Dependent Bean -->
<bean id="addressBean" class="Address">
<constructor-arg value="Chennai" />
<constructor-arg value="Tamil Nadu" />
<constructor-arg value="India" />
</bean>
<!-- Target Bean with Dependent Object Injected -->
<bean id="employeeBean" class="Employee">
<constructor-arg value="101" type="int" />
<constructor-arg value="Peter" type="java.lang.String" />
<constructor-arg ref="addressBean" />
</bean>
</beans>
4. Test the Bean Initialization
Load the application context with ClassPathXmlApplicationContext, fetch the bean, and print the output:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = (Employee) context.getBean("employeeBean");
emp.showEmployeeDetails();
}
}
Watch More Spring Tutorials
Click here to watch on Youtube:
https://www.youtube.com/watch?v=boxmf61NTTw&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
| How to Inject Dependent Object using Spring dependency injection by constructor? | Spring Tutorial |
| How to Inject Dependent Object using Spring dependency injection by constructor? | Spring Tutorial |
| How to Inject Dependent Object using Spring dependency injection by constructor? | Spring Tutorial |
| How to Inject Dependent Object using Spring dependency injection by constructor? | Spring Tutorial |
| How to Inject Dependent Object using Spring dependency injection by constructor? | Spring Tutorial |
| How to Inject Dependent Object using Spring dependency injection by constructor? | Spring Tutorial |