Spring Constructor Injection with collection

Spring Framework allows to inject primitive or Object references with collection.There are three XML elements available for this.

  • <list>
  • <set>
  • <map>

Syntax For Primitive values Collection

Here is syntax to define collection of primitive values.

 
<[list,set,map]> 
 <value>value here </value>
</[list,set,map]>

Syntax For Object values Collection

Here is syntax to define collection of object values.

 
<[list,set,map]>
<ref bean="bean id"/>
</[list,set,map]>

In this example, we are taking the example of Customer and Address where Customer one or more addresses.

Customer.java

package com.startwithjava.beans;

import java.util.List;

public class Customer {
	private String name;
	private String email;
	private List<Address> addresslist;
	public Customer() {
		super();
	}
	public Customer(String name,String email,List<Address> addresslist) {
		super();
		this.name  = name;
		this.email = email;
		this.addresslist = addresslist;
	}
	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 List<Address> getAddress() {
		return addresslist;
	}
	public void setAddress(List<Address> addresslist) {
		this.addresslist = addresslist;
	}
       public void display(){
         System.out.println("Name :"+name);
         System.out.println("Name :"+email);
         Iterator<Address> itr = addresslist.iterator();
         while(itr.hasNext()){
            Address address = itr.next();
            System.out.println("City:"+address.getCity()+"\tZip COde:"+address.getZipcode());
         }
       }
}

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;
	}
}

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="address1" class="com.startwithjava.beans.Address">
    <constructor-arg value="New Delhi"/>
    <constructor-arg value="Delhi"/>
    <constructor-arg value="110083"/>
    </bean>  
   <bean id="address2" class="com.startwithjava.beans.Address">
    <constructor-arg value="New Delhi"/>
    <constructor-arg value="Delhi"/>
    <constructor-arg value="110092"/>
    </bean>  
    <bean id="cust1" class="com.startwithjava.beans.Customer">  
    <constructor-arg value="Raj"></constructor-arg>  
    <constructor-arg value="raj123@gmail.com"></constructor-arg>  
    <constructor-arg>  
    <list>  
     <ref bean="address1"/>
     <ref bean="address2"/>
    </list>  
    </constructor-arg>  
    </bean>  
      
    </beans>  

Test.java

package com.startwithjava.test;
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);  
    Customer customer=(Customer)factory.getBean("cust1");  
    q.display();    
}  
}  

Leave a Reply