Spring Setter Injection

What is Setter Injection in Spring

Setter Injection in Spring is a type of dependency injection in which the framework injects the dependent objects into the client using a setter method. The container first calls the no argument constructor and then calls the setters. The setter based injection can work even If some dependencies have been injected using the constructor.
Github – Source Code for this example
Setter Injection In spring

Example of Setter Injection in Spring

In typical software development, classes collaborate with each other to achieve a desired functionality. e.g. In an ATM (Automated Teller Machine) system, the ATM class and Printer class can collaborate with each other to print the balance information for a bank account.

Setter Injection in spring

Collaboration between classes is usually expressed as dependency where the reference of one class is held by another class. e.g. The ATM class holds a reference to Printer class (see line 3).

            public class ATM {

            private Printer printer;

The dependencies need to be ‘resolved’ before the desired functionality is can be achieved. e.g. By ‘resolved’ we mean that an instance of Printer class needs to be created and associated with the ‘printer’ member in ATM class. When dependency resolution is not performed by the class itself but is left to be done by an external agent (e.g. Spring Framework) it is called dependency injection. So in our example, Spring will create an instance of the Printer class and associate the instance with the ‘printer’ member in ATM class. However as the ‘printer’ member in ATM class is private, the ATM class needs to expose its dependency to Spring for it to inject the Printer instance into the ATM class. e.g. If the ATM class exposes its dependency on Printer class as a setter method so that Spring can inject the Printer object then this is called as Setter injection (see line 12 below).

Now, Spring can create an instance of Printer class and inject it via the setter method defined in ATM class.

Construction injection or Setter Injection ?

Setter method injection is a very common way to resolve dependencies within the Spring framework. Hence, although constructor injection is possible using Spring framework, setter method injection is the most favored way to resolve dependencies using Spring framework.

Sample Program for Setter Injection in spring

Program Overview

This program uses spring framework’s setter injection mechanism to inject and instance of Printer class into ATM.
Here’s the pom file.

Source Code for Setter injection in spring example

Create a Printer class with a method printBalanceInformation()

Create the ATM class with a dependency on Printer class. The ATM class delegates the call to print the balance information to the Printer class (see line 16 in the ATM class above). Now before Spring framework can perform setter injection, it needs to know about the ATM and Printer classes. This is achieved by declaring the ATM and Printer classes as Spring beans in spring-config.xml (see line 10 and line 14 below).Note that setter injection is achieved by passing a reference of the ‘printer’ class to ATM class bean definition(see line 11 below)

Finally, we need a java program to test our setter dependency injection setup. This is done by TestSetterDependencyInjection.java (see source code below). We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans(see line 11 below). We get the reference to ATM class through Spring using the bean name ‘atm’ (see line 12 below). In this step, Spring will resolve the dependency on Printer class by injecting it using setter injection. We call the printBalanceInformation() on ATM class (see line 14 below) with some accountNumber (see line 13 below).

This completes our tutorial on Setter Injection in Spring. In the next tutorial, we will look at a less used form of injection – spring inner bean injection.

2 thoughts on “Spring Setter Injection”

  1. I have searched internet for a long period to understand dependency Injection. Finally clearly understood in your tutorial. Thank you so much.


Leave a Comment