Spring Constructor Injection for Dependent Object

HAS-A relationship between the classes requires reference of another object to statisfy its dependency.

<ref bean=”bean-name”/> is sub-element of <constructor-arg> is used to provide the reference of another bean.

Here in our case there’s Customer class which has dependency on Address class.Lets example here to understand scenarios.
Add the jar files of Spring core module to your project classpath.

Address.java

package com.startwithjava.beans;

public class Address {
	private String city;
	private String State;
	private String zipcode;
	
	public Address() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Address(String city, String state, String zipcode) {
		super();
		this.city = city;
		State = state;
		this.zipcode = zipcode;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return State;
	}
	public void setState(String state) {
		State = state;
	}
	public String getZipcode() {
		return zipcode;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	

}

Customer.java

package com.startwithjava.beans;

public class Customer {
	private String name;
	private String email;
	private String zipcode;
	private Address address;
	public Customer() {
		super();
	}
	public Customer(String name, Address address) {
		super();
		this.name = name;
		this.address = address;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getZipcode() {
		return zipcode;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public void display(){	
		 System.out.println("Name:-"+name);
		 System.out.println("City:"+address.getCity());
		 System.out.println("State:"+address.getState());
		 System.out.println("Zip Code:"+address.getZipcode()); 
	}
	
	

}

applicationContext.xml

<?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="customerbean" class="com.startwithjava.beans.Customer">   
<constructor-arg value="Raj"></constructor-arg> 
<constructor-arg>
<ref bean="address"/>  
</constructor-arg> 
</bean> 
<bean id="address" class="com.startwithjava.beans.Address">
<constructor-arg value="New Delhi"></constructor-arg> 
<constructor-arg value="Delhi"></constructor-arg> 
<constructor-arg value="110083"></constructor-arg>
</bean>
</beans>

TestIOCInjection.java

package com.startwithjava;

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;

import com.startwithjava.beans.Customer;

public class TestIOCInjection {
	 public static void main(String[] args) {  
		  Resource resource=new ClassPathResource("applicationContext.xml");  
		    BeanFactory factory=new XmlBeanFactory(resource);  
		      
		    Customer c=(Customer)factory.getBean("customerbean");  
		    c.display();  
	          
	    }  
}

Leave a Reply