Injecting this object into another object in Spring is very common when you work with projects that use the Spring framework. In my previous tutorial, I mentioned Spring’s ref attribute to include a Shape object into a Drawing object, in this tutorial I’ll talk about some other ways to inject this object into another object in Spring using an XML file.
First, I will create a new Maven project as an example:
I will use Java 17 for this example application:
1 2 3 4 |
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> |
Spring dependency is as follows:
1 2 3 4 5 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.2</version> </dependency> |
OK, now suppose you have a class, this class will contain one or more students, specifically as follows:
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 |
package com.huongdanjava.springexample; public class Student { private String name; private int age; public Student(String name) { this.name = name; } public Student(int age) { this.age = age; } public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } } |
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 |
package com.huongdanjava.springexample; import java.util.List; public class Clazz { private Student student; private List<Student> students; public Clazz(Student student) { this.student = student; } public Clazz(List<Student> students) { this.students = students; } public Student getStudent() { return student; } public List<Student> getStudents() { return students; } } |
Now let’s look at the first case. I will create a class with only one student by putting student object A in the Clazz object. Normally, we will initialize as follows:
1 2 3 4 5 6 7 |
<bean id="studenta" class="com.huongdanjava.springexample.Student"> <constructor-arg value="A" /> </bean> <bean id="clazz" class="com.huongdanjava.springexample.Clazz"> <constructor-arg type="com.huongdanjava.springexample.Student" ref="studenta" /> </bean> |
If our student object A declares a name, we can also use it with Spring’s ref attribute.
1 2 3 4 5 6 7 |
<bean id="clazz" class="com.huongdanjava.springexample.Clazz"> <constructor-arg type="com.huongdanjava.springexample.Student" ref="student1" /> </bean> <bean id="studenta" class="com.huongdanjava.springexample.Student" name="student1"> <constructor-arg value="A" /> </bean> |
In addition, we have another way to declare:
1 2 3 4 5 6 7 |
<bean id="clazz" class="com.huongdanjava.springexample.Clazz"> <constructor-arg type="com.huongdanjava.springexample.Student"> <bean id="studenta" class="com.huongdanjava.springexample.Student"> <constructor-arg value="A" /> </bean> </constructor-arg> </bean> |
Student object A created like this, called Inner Bean. The id of the bean, in this case, does not matter, Spring does not care about it. You should only apply it in some special cases. From my point of view, we should not use it. Spring supports it, then I just want to tell you guys about it 😀
Code to test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava.springexample; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { // Call Spring container ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); // Request to get Clazz object Clazz clazz = (Clazz) context.getBean("clazz"); // Do anything System.out.println(clazz.getStudent().toString()); } } |
Result:
OK, now I’ll talk about the second case. We will create a class with a list of students. I will create three students A, B, C, and put them into the object Clazz. As follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<bean id="clazz" class="com.huongdanjava.springexample.Clazz"> <constructor-arg type="java.util.List"> <list> <ref bean="studenta"/> <ref bean="studentb"/> <ref bean="studentc"/> </list> </constructor-arg> </bean> <bean id="studenta" class="com.huongdanjava.springexample.Student"> <constructor-arg value="A" /> </bean> <bean id="studentb" class="com.huongdanjava.springexample.Student"> <constructor-arg value="B" /> </bean> <bean id="studentc" class="com.huongdanjava.springexample.Student"> <constructor-arg value="C" /> </bean> |
Code to test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava.springexample; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { // Call Spring container ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); // Request to get Clazz object Clazz clazz = (Clazz) context.getBean("clazz"); // Do anything System.out.println(clazz.getStudents().size()); } } |
Result: