CodesJava

Easy learning with example program codes

Spring dependency injection collections


Spring framework provides the facility to inject collection values via constructor or setter method. We can use the following inside the constructor or property element.


1. List.
2. Set.
3. Map.

Syntax (constructor based dependency injection):

<bean id="testBeanId" class="Test">  
   <constructor-arg>  
    <list>  
     <value>value1</value>  
     <value>value2</value>  
     <value>value3</value>  
   </list>  
  </constructor-arg>  
</bean>

Syntax (setter based dependency injection):

<bean id="testBeanId" class="Test">  
   <property name="testProperty">  
    <list>  
     <value>value1</value>  
     <value>value2</value>  
     <value>value3</value>  
   </list>  
  </property>  
</bean>

Example Explanation:

We have created two beans “Student” and “Address”. Student class requires an Address class object. In spring configuration file we define Address bean objects and pass these objects as a list in constructor-arg.

Example:

Student.java

package com.codesjava.business;
 
import java.util.List;
 
/**
 * This class will be used as a bean class.
 * @author codesjava
 */
public class Student {
	private String name;
	private String rollNo;
	private String className;
	private List<Address> address;
 
	public Student(List<Address> address){
		this.address = (List<Address>) address;
	}
 
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getRollNo() {
		return rollNo;
	}
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public List<Address> getAddress() {
		return address;
	}	
}

Address.java

package com.codesjava.business;
 
/**
 * This class will be used as a bean class.
 * @author codesjava
 */
public class Address {
	private String addLine;
	private String city;
	private String state;
	private String country;
 
	public String getAddLine() {
		return addLine;
	}
	public void setAddLine(String addLine) {
		this.addLine = addLine;
	}
	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;
	}	
}

applicationContext.java

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
   <bean id="student" class="com.codesjava.business.Student">
   	   <property name="name" value="Jai"/>
       <property name="rollNo" value="MCA/07/06"/>
       <property name="className" value="MCA"/>
       <constructor-arg>
        <list>
      	 <ref bean="address1"/>
         <ref bean="address2"/>
        </list>
       </constructor-arg>
   </bean>
 
    <bean id="address1" class="com.codesjava.business.Address">
       <property name="addLine" value="Test address1"/>
       <property name="city" value="Gurgaon"/>
       <property name="state" value="Haryana"/>
       <property name="country" value="India"/>
    </bean>
 
    <bean id="address2" class="com.codesjava.business.Address">
       <property name="addLine" value="Test address2"/>
       <property name="city" value="Panipat"/>
       <property name="state" value="Haryana"/>
       <property name="country" value="India"/>
    </bean>
 
</beans>

Test.java

package com.codesjava.business;
 
import java.util.List;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
 public static void main(String[] args) {
	//Get ApplicationContext using spring configuration file.
	ApplicationContext context = 
	  new ClassPathXmlApplicationContext("applicationContext.xml");
 
	//Get Student bean object from ApplicationContext instance. 
	Student student = (Student) context.getBean("student");
 
	//Process Student Object.
	System.out.println("Student info: ");
	System.out.println("Name: " + student.getName());
	System.out.println("RollNo: " + student.getRollNo());
	System.out.println("Class: " + student.getClassName());
 
	//Get Address from Student Object.
	List<Address> studentAddressList = student.getAddress();
 
	//Declare program counter.
	int addressCounter = 1;
 
	//Iterate Address List.
	for (Address studentAddress : studentAddressList) {
	  //Process Address Object.
	  System.out.println("Student Address " +addressCounter+ ": ");
	  System.out.println("Address Line: "+studentAddress.getAddLine());
	  System.out.println("City: " + studentAddress.getCity());
          System.out.println("State: " + studentAddress.getState());
	  System.out.println("Country: " + studentAddress.getCountry());
	  addressCounter++;
       }			
  } 
}

Output:

Student info: 
Name: Jai
RollNo: MCA/07/06
Class: MCA
Student Address 1: 
Address Line: Test address1
City: Gurgaon
State: Haryana
Country: India
Student Address 2: 
Address Line: Test address2
City: Panipat
State: Haryana
Country: India

 
Download this example.
 

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki