In this guide, we will give you a glimpse of how to perform the Constructor Dependency injection with the dependent object in Spring Framework. If the two classes are related with a HAS-A relationship, we create the instance of a dependent object (contained object) and pass it as an argument of the main class constructor. Let’s better understand this with an example:
Employee HAS-A Address
Here, The Address class object is defined as the dependent object. Now, first of all, we will study the address class.
This class is made up of three properties, one constructor and a toString() method to return the values of these objects.
package com.intellinuts; public class Address { private String city; private String state; private String country; public Address(String city, String state, String country) { super(); this.city = city; this.state = state; this.country = country; } public String toString(){ return city+" "+state+" "+country; } }
Employee.java class is made up of three properties that are id, name, and address(dependent object), two constructors, and a show() method to display the records of the current object containing the dependent object.
package com.intellinuts; public class Employee { private int id; private String name; private Address address;//Aggregation public Employee() {System.out.println("def cons");} public Employee(int id, String name, Address address) { super(); this.id = id; this.name = name; this.address = address; } void show(){ System.out.println(id+" "+name); System.out.println(address.toString()); } }
Here, the ref attribute is used to assign a reference of another object, as we are passing the dependent object as a constructor argument.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="a1" class="com.intellinuts.Address"> <constructor-arg value="ghaziabad"></constructor-arg> <constructor-arg value="UP"></constructor-arg> <constructor-arg value="India"></constructor-arg> </bean> <bean id="e" class="com.intellinuts.Employee"> <constructor-arg value="12" type="int"></constructor-arg> <constructor-arg value="Sonoo"></constructor-arg> <constructor-arg> <ref bean="a1"/> </constructor-arg> </bean> </beans>
Test.java class is used to retrieve the bean from the applicationContext.xml file and call the show() method.
package com.intellinuts; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.*; public class Test { public static void main(String[] args) { Resource r=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(r); Employee s=(Employee)factory.getBean("e"); s.show(); } }
The output will be:
Employee ID: 12 Name: Sonoo
City is Ghaziabad State is UP and Country is India