Setter Dependency Injection

As we all know by this time that , there are 2 types of DI in Spring based on how we inject the dependent beans
They are as below
1) Constructor DI
2) Setter DI

We have already learned about Constructor DI, now we will see Setter DI.

Setter DI is the method of injecting dependent beans through Setters in the class

It means, we create setter for each dependent object and pass the dependent object as parameter to the corresponding setter and wire them accordingly in the spring configuration file.

We can also inject primitive values through setter.

Let us see below example

We have Person and Country classes where each person will have an associated country.

So Person is dependent on Country.

Create a Person class

It contains 3 primitive attributes and 1 dependent object reference to Country.
Person class has 4 setter methods one for each attribute and each setter method takes the corresponding parameter.
These parameters are injected in the spring configuration file.

There is also a toString() method which returns the values of Person

  1. package com.kb.di;
  2.  
  3. public class Person {
  4.     private int id;
  5.     private String name;
  6.     private int age;
  7.     private Country country;
  8.  
  9.    
  10.     public void showPerson(){
  11.     System.out.println(toString());
  12.     System.out.println(country.toString());
  13.     }
  14.  
  15.     @Override
  16.     public String toString() {
  17.         return "Person details -> id "+id+" name "+name+" age "+age;
  18.     }
  19.  
  20.     public void setId(int id) {
  21.         this.id = id;
  22.     }
  23.  
  24.     public void setName(String name) {
  25.         this.name = name;
  26.     }
  27.  
  28.     public void setAge(int age) {
  29.         this.age = age;
  30.     }
  31.  
  32.     public void setCountry(Country country) {
  33.         this.country = country;
  34.     }
  35. }
package com.kb.di;

public class Person {
	private int id;
	private String name;
	private int age;
	private Country country;

	
	public void showPerson(){
	System.out.println(toString());
	System.out.println(country.toString());
	}

	@Override
	public String toString() {
		return "Person details -> id "+id+" name "+name+" age "+age;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public void setCountry(Country country) {
		this.country = country;
	}
}

Create a Country class

It contains 2 primitive attributes

Country class has 2 setter methods one for each attribute and each setter takes the corresponding parameter and these parameters are injected in the spring configuration file.

There is also a toString() method which returns the values of Country

  1. package com.kb.di;
  2.  
  3. public class Country {
  4.     private int id;
  5.     private String name;
  6.  
  7.    
  8.     @Override
  9.     public String toString() {
  10.         return "Country details -> id "+id+" name "+name;
  11.     }
  12.  
  13.  
  14.     public void setId(int id) {
  15.         this.id = id;
  16.     }
  17.  
  18.  
  19.     public void setName(String name) {
  20.         this.name = name;
  21.     }
  22.    
  23. }
package com.kb.di;

public class Country {
	private int id;
	private String name;

	
	@Override
	public String toString() {
		return "Country details -> id "+id+" name "+name;
	}


	public void setId(int id) {
		this.id = id;
	}


	public void setName(String name) {
		this.name = name;
	}
	
}

Create a Spring configuration file, beans.xml

< property > tag is used to inject the values through setter

name attribute inside < property > tag is used to specify name of the attribute.

value attribute inside < property > tag is used to specify the value.
It should exactly match with the name defined in java class

ref is used to specify the reference to another bean with its id.
In this case, it is referring to country.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
  6.  
  7.     <bean id="country" class="com.kb.di.Country">
  8.         <property name="id" value="111"></property>
  9.         <property name="name" value="India"></property>
  10.     </bean>
  11.  
  12.  
  13.     <bean id="person" class="com.kb.di.Person">
  14.         <property name="id" value="1"></property>
  15.         <property name="name" value="Raj"></property>
  16.         <property name="age" value="22"></property>
  17.         <property name="country" ref="country"></property>
  18.  
  19.     </bean>
  20. </beans>
<?xml version="1.0" encoding="UTF-8"?>
<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-3.2.xsd">

	<bean id="country" class="com.kb.di.Country">
		<property name="id" value="111"></property>
		<property name="name" value="India"></property>
	</bean>


	<bean id="person" class="com.kb.di.Person">
		<property name="id" value="1"></property>
		<property name="name" value="Raj"></property>
		<property name="age" value="22"></property>
		<property name="country" ref="country"></property>

	</bean>
</beans>

Note : In setter injection, we don’t specify the data type of a property value unlike we do it in construction injection if it is non string primitive type.

create Pom.xml

Spring core related dependencies are specified in this pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.  
  5.   <groupId>Spring</groupId>
  6.   <artifactId>SpringCore</artifactId>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <packaging>jar</packaging>
  9.  
  10.   <name>SpringCore</name>
  11.   <url>http://maven.apache.org</url>
  12.  
  13.   <properties>
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15.   </properties>
  16.  
  17.   <dependencies>
  18.   <dependency>
  19.     <groupId>org.springframework</groupId>
  20.     <artifactId>spring-core</artifactId>
  21.     <version>4.2.4.RELEASE</version>
  22. </dependency>
  23.   <dependency>
  24.     <groupId>org.springframework</groupId>
  25.     <artifactId>spring-context</artifactId>
  26.     <version>4.2.4.RELEASE</version>
  27. </dependency>
  28.  
  29.     <dependency>
  30.       <groupId>junit</groupId>
  31.       <artifactId>junit</artifactId>
  32.       <version>3.8.1</version>
  33.       <scope>test</scope>
  34.     </dependency>
  35.   </dependencies>
  36. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Spring</groupId>
  <artifactId>SpringCore</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringCore</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Create a SetterDI class

This class loads the spring beans using application context and calls the person class method showPerson() to display the person details.

  1. package com.kb.di;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class SetterDI {
  7.    
  8.     public static void main(String[] args) {
  9.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
  10.         Person person = (Person)applicationContext.getBean("person");
  11.         person.showPerson();
  12.    
  13.     }
  14. }
package com.kb.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SetterDI {
	
	public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");  
        Person person = (Person)applicationContext.getBean("person");
        person.showPerson();
    
	}
}

Now right click on SetterDI class and run as java application

Output

Download this project SetterDI.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on