Injecting this object into another object in Spring is very common when you work with projects that using 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 some other ways to inject this object into another object in Spring using XML file.
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 |
package com.huongdanjava.springexample; 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 to the above declaration, we can also declare as follows:
1 2 3 4 5 |
<bean id="clazz" class="com.huongdanjava.springexample.Clazz"> <constructor-arg type="com.huongdanjava.springexample.Student"> <idref bean="studenta"/> </constructor-arg> </bean> |
The difference between the ref attribute and the idref attribute is that: with ref attribute, you can declare the bean’s id or the bean’s name, but for idref, it is only possible to declare the bean’s id.
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. Id of the bean, in this case, it does not matter, Spring does not care about it. You should only apply it in some special case. fFrom 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 20 |
package com.huongdanjava.springexample; public class Application { /** * @param args */ 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:
1 |
Student [name=A, age=0] |
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; public class Application { /** * @param args */ 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:
1 |
3 |