Constructor Injection with Dependent Object

Constructor Injection with Dependent Object

This article contains detailed description with example on “Constructor Injection with Dependent Object” topic.

Whenever we have HAS-A relationship between the classes, instance of dependent object comes into the picture. We create the instance of dependent object (contained object) first then pass it as an argument of the main class constructor.

In this example we will discuss about constructor injection with dependent object using a simple and self explanatory program.

This class contains three properties, one constructor and toString() method to return the values of these object.

Address.java

package com.codeNuclear;

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:"+city+" State:"+state+" Country:"+country;
	}
}
Students.java

It contains three properties id, name and address(dependent object) ,constructors and show() method to show the records of the current object including the depedent object.


package com.codeNuclear;

public class Students 
{
	private int id;
	private String name;
	private Address address; //Aggregation
	
	public Students(int id,String name,Address address)
	{
		super();
		this.id = id;
		this.name = name;
		this.address = address;
	}
	
	void show()
	{
		System.out.println("ID:" +id+" "+"Name:" +name);
		System.out.println(address.toString());
	}
}
applicationContext.xml

The ref attribute is used to define the reference of another object, such way we are passing the dependent object as an 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.codeNuclear.Address">  
<constructor-arg value="Pune"></constructor-arg>  
<constructor-arg value="Maharashtra"></constructor-arg>  
<constructor-arg value="India"></constructor-arg>  
</bean>  
  
  
<bean id="e1" class="com.codeNuclear.Students">  
<constructor-arg value="10" type="int"></constructor-arg>  
<constructor-arg value="Smith"></constructor-arg>  
<constructor-arg>  
<ref bean="a1"/>  
</constructor-arg>  
</bean>  
  
</beans> 
Test.java

This class gets the bean from the applicationContext.xml file and calls the show method.


package com.codeNuclear;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {

	public static void main(String[] args) 
	{
		Resource r=new ClassPathResource("applicationContext.xml");  
        BeanFactory factory=new XmlBeanFactory(r);  
          
        Students s=(Students)factory.getBean("e1");  
        s.show(); 

	}

}

Output ID:10 Name:Smith City:Pune State:New York Country:USA