Spring Setter-based Dependency Injection Example
In this Spring tutorial, we will learn Spring setter-based dependency injection. Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static
factory method to instantiate your bean.
To set the beans property value via setter method, we have <property/>
tag under the <bean/>
tag. And by using value
attribute or <value>Sandeep</value>
child tag, we can set the values.
Set values using value attribute.
<property name="name" value="Sandeep" />
Or you can set values using value child tag.
<property name="salary">
<value>120000</value>
</property>
You may be interested in Spring Constructor-based Dependency Injection Example
Dependencies Required
To develop Spring setter-based DI, we hardly need the four to five JARs of Spring framework. These JARs file name is given below.
- commons-logging-1.1.3.jar
- spring-beans-5.0.2.RELEASE.jar
- spring-context-5.0.2.RELEASE.jar
- spring-core-5.0.2.RELEASE.jar
- spring-expression-5.0.2.RELEASE.jar
Spring Beans
Create a simple POJO class Employee
, define the employee variables and generate the setter
method of all variables.
package org.websparrow.beans;
public class Employee {
// generate setter methods of all variables.
private String name;
private String email;
private int salary;
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setSalary(int salary) {
this.salary = salary;
}
// business logic that actually uses the injected values.
public void employeeDetails() {
System.out.println("Employee Name: " + name);
System.out.println("Employee Email: " + email);
System.out.println("Employee Salary: " + salary);
}
}
Spring Bean Configuration
Configure the Employee
class in bean tag and set the property values using property child tag in spring.xml.
<?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.xsd">
<bean id="emp" class="org.websparrow.beans.Employee">
<!-- setting value using value attribute -->
<property name="name" value="Sandeep" />
<property name="email" value="[email protected]" />
<!-- setting value using value child tag -->
<property name="salary">
<value>120000</value>
</property>
</bean>
</beans>
Execution
Finally, load the configuration file and run it.
package org.websparrow.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.beans.Employee;
public class Hr {
public static void main(String[] args) {
// load the spring.xml file
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Employee e = (Employee) context.getBean("emp");
e.employeeDetails();
}
}
After the successful compilation and execution, you will get the following output on your console log.
Employee Name: Sandeep
Employee Email: [email protected]
Employee Salary: 120000
Download Source Code: spring-setter-based-dependency-injection-example