- Spring DI - Home
- Spring DI - Overview
- Spring DI - Environment Setup
- Spring DI - Create Project
- Spring DI - IOC Containers
- Spring Dependency Injection
Constructor Based Injection Examples
- Spring DI - Constructor Based
- Spring DI - Inner Beans via Constructor
- Spring DI - Collections via Constructor
- Spring DI - Collection Ref via Constructor
- Spring DI - Map via Constructor
- Spring DI - Map Ref via Constructor
Setter Based Injection Examples
- Spring DI - Setter Based
- Spring DI - Inner Beans Setter
- Spring DI - Collections Setter
- Spring DI - Collection Ref Setter
- Spring DI - Map Setter
- Spring DI - Map Ref Setter
Autowiring Examples
- Spring DI - Autowiring
- Spring DI - Autowiring ByName
- Spring DI - Autowiring ByType
- Spring DI - Autowiring Constructor
Factory Method
Spring DI Useful Resources
Spring DI - Injecting Collection References via Constructor
You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.
Now what if you want to pass plural values like Java Collection types such as List, Set, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows −
| Sr.No | Element & Description |
|---|---|
| 1 |
<list> This helps in wiring ie injecting a list of values, allowing duplicates. |
| 2 |
<set> This helps in wiring a set of values but without any duplicates. |
You can use either <list> or <set> to wire any implementation of java.util.Collection or an array.
In this example, we're showcasing passing collection elements using ref.
Example - Usage of Constructor Refs Based DI of Collections
The following example shows a class JavaCollection that is using collection of dependencies injected using constructor.
Let's update the project created in Spring DI - Create Project chapter. We're adding following files −
Address.java − A class to be used as dependency.
JavaCollection.java − A class containing a collections of dependencies.
MainApp.java − Main application to run and test.
Address.java
Here is the content of Address.java file −
package com.tutorialspoint;
public class Address {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
JavaCollection.java
Here is the content of JavaCollection.java file −
package com.tutorialspoint;
import java.util.List;
import java.util.Set;
public class JavaCollection {
List<Address> addressList;
Set<Address> addressSet;
public JavaCollection(List<Address> addressList, Set<Address> addressSet) {
this.addressList = addressList;
this.addressSet = addressSet;
}
// a setter method to set List
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list.
public List<Address> getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set<Address> addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set<Address> getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
}
MainApp.java
Following is the content of the MainApp.java file −
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
}
}
applicationcontext.xml
Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −
<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 = "address1" class = "com.tutorialspoint.Address">
<property name="name" value="INDIA"></property>
</bean>
<bean id = "address2" class = "com.tutorialspoint.Address">
<property name="name" value="JAPAN"></property>
</bean>
<bean id = "address3" class = "com.tutorialspoint.Address">
<property name="name" value="USA"></property>
</bean>
<bean id = "address4" class = "com.tutorialspoint.Address">
<property name="name" value="UK"></property>
</bean>
<!-- Definition for javaCollection -->
<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
<constructor-arg name = "addressList">
<list>
<ref bean="address1" />
<ref bean="address2" />
<ref bean="address3" />
<ref bean="address4" />
</list>
</constructor-arg>
<constructor-arg name = "addressSet">
<set>
<ref bean="address1" />
<ref bean="address2" />
<ref bean="address3" />
<ref bean="address4" />
</set>
</constructor-arg>
</bean>
</beans>
Output
Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −
List Elements :[INDIA, JAPAN, USA, UK] Set Elements :[INDIA, JAPAN, USA, UK]