Spring Tutorial - Spring Setter Injection








Spring Setter injection is the most popular and simple dependency injection method, it will injects the dependency via a setter method.

In Setter Dependency Injection , the IoC container injects a Java Bean's dependencies via JavaBean-style setter methods.

A Java Bean's setters expose the dependencies the IoC container can manage.

In practice, Setter Injection is the most widely used injection mechanism, and it is one of the simplest IoC mechanisms to implement.

Example

Suppose we have the following interface and Java beans defined.

package com.java2s.common;
public interface Printer
{
  public void print();
}

After that we would create CSV printer which will output data in CSV format. The CSV printer implements the Printer interface.

package com.java2s.common;
public class CSVPrinter implements Printer
{
  public void print(){
    System.out.println("Csv Output Printer");
  }
}

Then it is time to create JSON printer which will output message in JSON format. The JSON printer also implements the Printer interface.

package com.java2s.common;
public class JSONPrinter implements Printer
{
  public void print(){
    System.out.println("Json Output Printer");
  }
}

By using Spring Dependency Injection (DI) we can declare Java Beans in the Spring configuration xml file. Then wire the Java Beans in the xml file. In this way Spring can make our printer loosely coupled to the different Printer implementations.

We change the Helper class to accept the Printer.

package com.java2s.common;
public class OutputHelper
{
  Printer outputGenerator;
  public void print(){
    outputGenerator.print();
  }
  public void setOutputGenerator(Printer outputGenerator){
    this.outputGenerator = outputGenerator;
  }
}




XML Configuration

Then we have to create a Spring bean configuration file and declare all your Java object dependencies here.

<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-2.5.xsd">
  <bean id="outputHelper" class="com.java2s.common.OutputHelper">
    <property name="outputGenerator" ref="csvPrinter" />
  </bean>
  <bean id="csvPrinter" class="com.java2s.common.CSVPrinter" />
  <bean id="jsonPrinter" class="com.java2s.common.JSONPrinter" />
</beans>

The following two bean tags declared two Java Beans in Spring configuration xml file.

After the declaration we can use the id value to reference the Java Beans.

<bean id="csvPrinter" class="com.java2s.common.CSVPrinter" />
<bean id="jsonPrinter" class="com.java2s.common.JSONPrinter" />

The following xml bean tag declared the OutputHelper and inject the dependency via setter injection by using property tag.

<bean id="outputHelper" class="com.java2s.common.OutputHelper">
  <property name="outputGenerator" ref="csvPrinter" />
</bean>

In the code above we just injected a 'com.java2s.common.CSVPrinter' bean into 'OutputHelper' object via a setter method setOutputGenerator.





Load configuration and Run

The following code shows how to use load the configuration and run it.

package com.java2s.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
        "SpringBeans.xml");
    OutputHelper output = (OutputHelper)context.getBean("outputHelper");
      output.print();
  }
}

Output



Download Java2s_Spring_Setter_Injection.zip