
Spring Constructor Injection with collection 2
Constructor injection with collections in Spring allows you to inject collections of objects, such as lists or sets, into a bean’s constructor. This is useful when you want to configure a bean with multiple dependencies of the same type. Here’s how to perform constructor injection with a collection in Spring:
1. Create Dependent Objects:
First, create the dependent objects (beans) that you want to inject into another bean’s constructor. These objects should be defined as separate Spring beans. For this example, let’s create a Student
class:
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
// Getter and setter for 'name'
}
2. Configure Beans in XML:
In your Spring configuration file (e.g., applicationContext.xml
), define the beans and specify the constructor injection by providing the constructor arguments as a collection. You can use <list>
, <set>
, or <map>
elements, depending on the type of collection you want to inject. In this example, we’ll use a <list>
to inject a list of Student
objects into another bean’s constructor:
<bean id="student1" class="com.example.Student">
<constructor-arg value="John" />
</bean>
<bean id="student2" class="com.example.Student">
<constructor-arg value="Alice" />
</bean>
<bean id="student3" class="com.example.Student">
<constructor-arg value="Bob" />
</bean>
<bean id="course" class="com.example.Course">
<constructor-arg>
<list>
<ref bean="student1" />
<ref bean="student2" />
<ref bean="student3" />
</list>
</constructor-arg>
</bean>
In this XML configuration:
- We define “student1,” “student2,” and “student3” beans of class
com.example.Student
with different names. - We define a “course” bean of class
com.example.Course
and inject a list of “student1,” “student2,” and “student3” beans as a constructor argument.
3. Create the Bean Receiving the Collection:
Now, create the bean that will receive the collection as a constructor argument. In this example, we’ll create a Course
class that receives a list of Student
objects:
import java.util.List;
public class Course {
private List<Student> students;
public Course(List<Student> students) {
this.students = students;
}
// Getter and setter for 'students'
}
4. Access the Target Bean:
You can access the target bean, “course,” by retrieving it from the Spring application context. The list of “student1,” “student2,” and “student3” beans will be injected into the Course
bean’s constructor when it is created.
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Course course = context.getBean("course", Course.class);
// Use the 'course' bean, which now has a list of 'Student' members
Constructor injection with collections allows you to pass multiple instances of objects to a bean’s constructor, making it suitable for scenarios where you need to configure a bean with a group of related dependencies. You can use the appropriate collection type (<list>
, <set>
, <map>
, etc.) based on your specific requirements.