Back to: Spring Framework Tutorials
Spring Framework Injecting Collections
In this article, I am going to discuss Spring Framework Injecting Collections with Examples. Please read our previous article, where we discussed Spring Framework Injecting Inner Beans.
What are Injecting Collections in Spring Framework?
Injecting collections in Spring refers to the process of injecting or populating a collection (such as a list, set, or map) into a bean’s property. This allows us to configure and manage collections of objects in Spring applications. Until now, we have been adding single properties in each bean. What if we need to add a list/set/collection? We do this by injecting collections.
Implementing Constructor Dependency Injection in Spring Framework
Before starting the project, ensure that the required JAR files are in the project’s “Referenced Libraries” folder.
Step 1: Create a new file called Students.java in the src/ directory. Add the following lines of code to the file:
import java.util.List; import java.util.Properties; import java.util.Set; public class Students { List<String> studentList; Set<String> studentSet; Properties studentProps; public List<String> getStudentList() { return studentList; } public void setStudentList(List<String> studentList) { this.studentList = studentList; } public Set<String> getStudentSet() { return studentSet; } public void setStudentSet(Set<String> studentSet) { this.studentSet = studentSet; } public Properties getStudentProps() { return studentProps; } public void setStudentProps(Properties studentProps) { this.studentProps = studentProps; } }
This class represents a POJO class which will be used to represent a set of students. In this example, we are using three different data structures to store students: list, set, and properties. Note that the setters and getters can be automatically added using the “Source Action…” option in the right-click menu of VS Code.
Step 2: In the src/ directory, there must be a file called App.java. Typically, this file is created automatically (when the project is created). This file contains the main() function. Modify the file as follows:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Students s = (Students) context.getBean("students"); System.out.println("List: " + s.getStudentList().toString()); System.out.println("Set: " + s.getStudentSet().toString()); System.out.println("Properties: " + s.getStudentProps().toString()); } }
The main function creates an object of type Students from the Beans.xml file. It then prints out each type of data structure.
Step 3: Create a new file called Beans.xml in the src/ directory. Add the following lines of code to the file:
<?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 = "students" class = "Students"> <property name = "studentList"> <list> <value>Student A</value> <value>Student B</value> <value>Student C</value> <value>Student D</value> </list> </property> <property name = "studentSet"> <set> <value>Student E</value> <value>Student F</value> <value>Student G</value> <value>Student H</value> </set> </property> <property name = "studentProps"> <props> <prop key = "one">Student I</prop> <prop key = "two">Student J</prop> <prop key = "three">Student K</prop> <prop key = "four">Student L</prop> </props> </property> </bean> </beans>
This file provides the parameters to create an object of type Students.
Step 4: Compile and execute the application. Ensure compilation is successful. Ensure that the output is as expected:
Congratulations! You have completed the injecting collections application in Spring Framework!
In the next article, I am going to discuss Spring Framework Autowiring. Here, in this article, I try to explain Spring Framework Injecting Collections with Examples. I hope you enjoy this Spring Framework Injecting Collections article.