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>

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.

  1. commons-logging-1.1.3.jar
  2. spring-beans-5.0.2.RELEASE.jar
  3. spring-context-5.0.2.RELEASE.jar
  4. spring-core-5.0.2.RELEASE.jar
  5. 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.

Employee.java
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.

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.

Hr.java
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();

	}
}
Output:

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

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.