In this post, We will learn about the Injecting Collections in Spring Framework Example using a Demo Project.
Here We will learn How to Inject List, Set, Map, Properties, and array.
We have already seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> and <constructor-arg> tags in our Spring bean configuration file. In both, cases we deal with passing a single value to a bean.
Now, what if We want to pass more than one value like Java Collection types such as List, Set, Map, Properties and Array. To handle this situation, Spring offers the following types of collection configuration elements which are as follows −
Sr.No | Element & Description |
---|---|
1 | <list>
This helps in wiring or injecting a list of values that allows duplicates. |
2 | <set>
This helps in wiring a set of unique values. |
3 | <map>
This we use to inject a collection of key-value pairs where key and value can be of any type. |
4 | <props>
This also can be used to inject a collection of key-value pairs where the key and value are both Strings. |
We may use either <list> or <array> to wire an array.
We may come across two situations (i) Passing direct values of the collection and (ii) Passing a reference of a bean as one of the collection elements.
Now Let’s try to understand the above concepts using the below demo Project.
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkhindigyan.org</groupId> <artifactId>InjectingCollectionExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kkhindigyan.model; public class Employee { private Integer employeeId; private String employeeName; private Double salary; private Address address; public Integer getEmployeeId() { return employeeId; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public void setAddress(Address address) { this.address = address; } public Address getAddress() { return address; } @Override public String toString() { return "Employee [employeeId=" + employeeId + ", employeeName=" + employeeName + ", salary=" + salary + "]"; } } |
Address.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.kkhindigyan.model; public class Address { private String addressLine1; private String addressLine2; private String city; private Long zipCode; public String getAddressLine1() { return addressLine1; } public void setAddressLine1(String addressLine1) { this.addressLine1 = addressLine1; } public String getAddressLine2() { return addressLine2; } public void setAddressLine2(String addressLine2) { this.addressLine2 = addressLine2; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Long getZipCode() { return zipCode; } public void setZipCode(Long zipCode) { this.zipCode = zipCode; } @Override public String toString() { return "Address [addressLine1=" + addressLine1 + ", addressLine2=" + addressLine2 + ", city=" + city + ", zipCode=" + zipCode + "]"; } } |
CollectionInfo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.kkhindigyan.info; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import com.kkhindigyan.model.Address; import com.kkhindigyan.model.Employee; public class CollectionInfo { private List<String> names; private List<Employee> employeeList; private Set<Employee> employeeSet; private Map<Employee,Address> employeeAddressMap; private Properties stateCaptital; private Employee employeesArray[] = new Employee[2]; public void setNames(List<String> names) { this.names = names; } public List<String> getNames() { return names; } public List<Employee> getEmployeeList() { return employeeList; } public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } public Set<Employee> getEmployeeSet() { return employeeSet; } public void setEmployeeSet(Set<Employee> employeeSet) { this.employeeSet = employeeSet; } public Map<Employee, Address> getEmployeeAddressMap() { return employeeAddressMap; } public void setEmployeeAddressMap(Map<Employee, Address> employeeAddressMap) { this.employeeAddressMap = employeeAddressMap; } public Properties getStateCaptital() { return stateCaptital; } public void setStateCaptital(Properties stateCaptital) { this.stateCaptital = stateCaptital; } public Employee[] getEmployeesArray() { return employeesArray; } public void setEmployeesArray(Employee[] employeesArray) { this.employeesArray = employeesArray; } } |
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="collectionInfo" class="com.kkhindigyan.info.CollectionInfo"> <property name="names"> <list> <value>KK</value> <value>PK</value> <value>KK</value> </list> </property> <property name="employeeList"> <list> <ref bean="employee1" /> <ref bean="employee2" /> <ref bean="employee1" /> </list> </property> <property name="employeeSet"> <set> <ref bean="employee1" /> <ref bean="employee2" /> <ref bean="employee1" /> </set> </property> <property name="employeeAddressMap"> <map> <entry key-ref="employee1" value-ref="address1"></entry> <entry key-ref="employee2" value-ref="address2"></entry> </map> </property> <property name="stateCaptital"> <props> <prop key="Maharashtra">Mumbai</prop> <prop key="Tamilnadu">Chennai</prop> </props> </property> <property name="employeesArray"> <!-- <array> <ref bean="employee1" /> <ref bean="employee1" /> </array> --> <list> <ref bean="employee1" /> <ref bean="employee1" /> </list> </property> </bean> <bean id="employee1" class="com.kkhindigyan.model.Employee"> <property name="employeeId" value="102883"></property> <property name="employeeName" value="Sean"></property> <property name="salary" value="80000"></property> <property name="address" ref="address1"></property> </bean> <bean id="employee2" class="com.kkhindigyan.model.Employee"> <property name="employeeId" value="102884"></property> <property name="employeeName" value="KK"></property> <property name="salary" value="90000"></property> <property name="address" ref="address2"></property> </bean> <bean id="address1" class="com.kkhindigyan.model.Address"> <property name="addressLine1" value="Address line 1"></property> <property name="addressLine2" value="Address line 2"></property> <property name="city" value="Delhi"></property> <property name="zipCode" value="959049"></property> </bean> <bean id="address2" class="com.kkhindigyan.model.Address"> <property name="addressLine1" value="Address line 1"></property> <property name="addressLine2" value="Address line 2"></property> <property name="city" value="Mumbai"></property> <property name="zipCode" value="879077"></property> </bean> </beans> |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package com.kkhindigyan.org; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kkhindigyan.info.CollectionInfo; import com.kkhindigyan.model.Address; import com.kkhindigyan.model.Employee; public class ClientTest { public static void main(String[] args) { AbstractApplicationContext applicationContext = null; try { applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); CollectionInfo collectionInfo = applicationContext.getBean(CollectionInfo.class); System.out.println("getNames------------------------------"); List<String> names = collectionInfo.getNames(); names.forEach(System.out::println); System.out.println("getEmployeeList------------------------------"); List<Employee> employeeList = collectionInfo.getEmployeeList(); employeeList.forEach(System.out::println); System.out.println("getEmployeeSet------------------------------"); Set<Employee> employeeSet = collectionInfo.getEmployeeSet(); employeeSet.forEach(System.out::println); System.out.println("getEmployeeAddressMap------------------------------"); Map<Employee, Address> employeeAddressMap = collectionInfo.getEmployeeAddressMap(); employeeAddressMap.forEach((employee,address)->{ System.out.println(employee); System.out.println(address); }); System.out.println("getStateCaptital------------------------------"); Properties stateCaptital = collectionInfo.getStateCaptital(); stateCaptital.forEach((state,capital)->{ System.out.println(state); System.out.println(capital); }); System.out.println("getEmployeesArray------------------------------"); Employee[] employeesArray = collectionInfo.getEmployeesArray(); for (Employee employee : employeesArray) { System.out.println(employee); } } catch (Exception e) { e.printStackTrace(); }finally { if(applicationContext != null) { applicationContext.close(); } } } } |
If you run ClientTest.java as Java Application then it will give the below output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
getNames------------------------------ KK PK KK getEmployeeList------------------------------ Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Employee [employeeId=102884, employeeName=KK, salary=90000.0] Employee [employeeId=102883, employeeName=Sean, salary=80000.0] getEmployeeSet------------------------------ Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Employee [employeeId=102884, employeeName=KK, salary=90000.0] getEmployeeAddressMap------------------------------ Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Address [addressLine1=Address line 1, addressLine2=Address line 2, city=Delhi, zipCode=959049] Employee [employeeId=102884, employeeName=KK, salary=90000.0] Address [addressLine1=Address line 1, addressLine2=Address line 2, city=Mumbai, zipCode=879077] getStateCaptital------------------------------ Maharashtra Mumbai Tamilnadu Chennai getEmployeesArray------------------------------ Employee [employeeId=102883, employeeName=Sean, salary=80000.0] Employee [employeeId=102883, employeeName=Sean, salary=80000.0] |
That’s all about Injecting Collections in Spring Framework Example
You May Also Like:
Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?
If you have any feedback or suggestion please feel free to drop in below comment box.