Setter Injection with Dependent Object

Setter Injection with Dependent Object

We can inject dependency of another class object into current bean using setter injection.In such case, we use property element. Here, our scenario is Employee HAS-A Address. The Address class object will be termed as the dependent object. Let’s see the Address class first:

Address.java

This class contains four properties, setters and getters and toString() method.


package com.codeNuclear;

public class Address 
{
	private String addressLine,city,state,country;

	public String getAddressLine() {
		return addressLine;
	}

	public void setAddressLine(String addressLine) {
		this.addressLine = addressLine;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}
	
	public String toString()
	{
		return addressLine+" "+city+" "+state+" "+country;
	}
}
Students.java

It contains three properties id, name and address(dependent object) , setters and getters with display() method.


package com.codeNuclear;

public class Students 
{
	private Address address;
	private int id;
	private String name;
	
	
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	void display()
	{
		System.out.println("ID:"+id+" "+"Name:"+name);
		System.out.println("Student Address:"+address);
	}
	
}
applicationContext.xml

The ref attribute of property elements is used to define the reference of another bean.


<?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="address1" class="com.codeNuclear.Address">  
<property name="addressLine" value="51,Broadway"></property>  
<property name="city" value="CANBERRA"></property>  
<property name="state" value="WEST BEACH"></property>  
<property name="country" value="Australia"></property>  
</bean>  
  
<bean id="obj" class="com.codeNuclear.Students">  
<property name="id" value="11"></property>  
<property name="name" value="Smith John"></property>  
<property name="address" ref="address1"></property>  
</bean>  
  
</beans>
Test.java

This class gets the bean from the applicationContext.xml file and calls the display() 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 e=(Students)factory.getBean("obj");  
	    e.display();

	}

}