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">

Dependencies Required

To develop Spring setter-based DI, we hardly need the four to five JARs of Spring framework. These JARs file name is give 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.

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"
	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="abc@example.com" />

		<!-- setting value using value child tag -->
		<property name="salary">




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");


After the successfully compilation and execution, you will get the following output on your console log.

Employee Name: Sandeep
Employee Email: abc@example.com
Employee Salary: 120000

Share this article on: