Till now, we inject the dependencies from the spring configuration file as simple types like primitives or wrapper classes (Objects) through the <properties> tag. But in Spring we can define the dependencies for three different types.
- Simple Type
- Object Type and
- Collection Type
In the previous Spring examples, we injected the dependencies using the simple types (int, string, object). But in this address you to implement spring collection dependency.
Spring Collection Dependency :
In spring we can inject the dependencies as a type of collection. But we have some limitations on that. i.e. Spring container can inject collection type, if and only if the collection type is one of the below 4.
- java.util.List
- java.util.Set
- java.util.Map
- java.util.Properties
We can say that the above four are spring collection types. Apart from those four types, we can not implement any collection type in spring.
Spring Collection List Example :
If the dependency type is java.util.List, then in spring configuration file we should configure the List type with <list>
tag.
If that list contains the normal properties like String or int or float, we can define the values using the <value>
tag.
If the list contains the object type, we can assign the bean to that object by using the <ref>
tag.
Below is the example for Lists contains normal properties and Object types.
Project Structure :
Spring Maven Dependency:
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
Spring Collection Beans :
Employee.Java
package com.onlinetutorialspoint.bean;
public class Employee {
private int empId;
private String empName;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
Department.java
package com.onlinetutorialspoint.bean;
import java.util.List;
public class Department {
private int deptId;
private String deptName;
private List<Employee> employees;
private List<String> telephoneNumbers;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public List<String> getTelephoneNumbers() {
return telephoneNumbers;
}
public void setTelephoneNumbers(List<String> telephoneNumbers) {
this.telephoneNumbers = telephoneNumbers;
}
}
Spring Collection Configuration:
spring configuration.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="deptBean" class="com.onlinetutorialspoint.bean.Department">
<property name="deptId" value="20501" />
<property name="deptName" value="IT" />
<property name="telephoneNumbers">
<list>
<value>0409696969</value>
<value>0409898989</value>
</list>
</property>
<property name="employees">
<list>
<ref bean="empBean" />
<ref bean="empBean2" />
<ref bean="empBean3" />
</list>
</property>
</bean>
<bean id="empBean" class="com.onlinetutorialspoint.bean.Employee">
<property name="empId" value="101" />
<property name="empName" value="Chandra Shekhar" />
</bean>
<bean id="empBean2" class="com.onlinetutorialspoint.bean.Employee">
<property name="empId" value="102" />
<property name="empName" value="Robert" />
</bean>
<bean id="empBean3" class="com.onlinetutorialspoint.bean.Employee">
<property name="empId" value="103" />
<property name="empName" value="John" />
</bean>
</beans>
Run the Application :
Main.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.onlinetutorialspoint.bean.Department;
import com.onlinetutorialspoint.bean.Employee;
public class Main {
public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext("springconfiguration.xml");
Department department = (Department) context.getBean("deptBean");
System.out.println("DeptId : " + department.getDeptId());
System.out.println("DeptName : " + department.getDeptName());
List<String> telephoneNumbers = department.getTelephoneNumbers();
int i = 1;
for (String number : telephoneNumbers) {
System.out.println("Telephone " + i + " :" + number);
i++;
}
System.out.println("===================");
System.out.println("Employees Under Department No : "+department.getDeptId());
List<Employee> employees = department.getEmployees();
for (Employee employee : employees) {
System.out.println("Employee Id : " + employee.getEmpId());
System.out.println("Empoyee Name : " + employee.getEmpName());
}
}
}
Output :
DeptId : 20501
DeptName : IT
Telephone 1 :0409696969
Telephone 2 :0409898989
===================
Employees Under Department No : 20501
Employee Id : 101
Empoyee Name : Chandra Shekhar
Employee Id : 102
Empoyee Name : Robert
Employee Id : 103
Empoyee Name : John
Happy Learning 🙂