In the previous tutorial, I already introduced you all the basic knowledge about Setter Injection in Spring using an XML file. In this tutorial, I will mention more details about it to help you can use it easily!
Let’s consider the following example:
I have the Student class:
1 2 3 4 |
package com.huongdanjava.springsetterinjection; public class Student { } |
and the Clazz class with the following content:
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 27 28 |
package com.huongdanjava.springsetterinjection; public class Clazz { private String name; private Student student; public String getName() { return name; } public void setName(String name) { this.name = name; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } @Override public String toString() { return "Clazz [name=" + name + ", student=" + student + "]"; } } |
As you see, Clazz class contains 2 properties: an instance of the Student class, a variable type String, and some Setter methods to assign the value to these 2 properties. So, how we can inject these objects into class Clazz using the Setter methods?
First of all, I will create a new Maven project:
with class Application has the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.springsetterinjection; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { BeanFactory context = new ClassPathXmlApplicationContext("spring.xml"); Clazz clazz = (Clazz) context.getBean("clazz"); System.out.println(clazz.toString()); } } |
I will use Java 17 for this example:
1 2 3 4 |
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> |
Spring dependency:
1 2 3 4 5 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.2</version> </dependency> |
OK, let’s get started.
To inject the Student object into the Clazz object, first, we need to initialize the Student object in the Spring container:
1 |
<bean id="student" class="com.huongdanjava.springsetterinjection.Student" /> |
And now, to initialize the Clazz object in the Spring container, I will declare as below:
1 2 3 4 |
<bean id="clazz" class="com.huongdanjava.springsetterinjection.Clazz" > <property name="name" value="Khanh" /> <property name="student" ref="student" /> </bean> |
In the above declaration, I used <property> tag with pair of attributes <name, value> and <name, ref>. Inside:
- The name attribute is mandatory, it is the name of the property which we need to inject into the Clazz object.
- Value and ref attributes are the same as Constructor Injection. The value attribute is used in case injecting value is a text, number or boolean … Ref attribute is used to refer to another bean which already been created in the Spring container.
Result:
Besides that, same like Constructor Injection, Spring also supports us namespace p to declare Setter Injection simpler.
This namespace allows us can declare properties of objects inside the <bean> tag as attributes. There is no XSD schema file for this namespace. To use it, you only need to declare in the XML configuration file of Spring as below:
1 2 3 4 5 6 7 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans> |
then using the following syntax to inject the dependency objects.
You should declare an attribute in <bean> tag beginning with “p:”, next is the name of an attribute of the object and end with “-ref” in case the dependency object is other beans in Spring container, then assign for it a value. For example, I can declare the Clazz object as below:
1 |
<bean id="clazz" class="com.huongdanjava.springsetterinjection.Clazz" p:name="Khanh" p:student-ref="student" /> |
Result: