Constructor Injection and Setter Injection Using XML Configuration

Setter injection and constructor injection are two of the ways in which we can inject data into an object through xml configuration files. We will reuse the same examples we were working on till now for this note also. 

 

Setter Injection (Property Initialization)

If you have a property in your bean class, you can initialize it using the property tag within the bean tag.

Add a variable called ‘data’ with getters and setters. Also add a new print statement to print the value of the variable data.

public class JJWriter {

  private String data;

  public String getData() {

    return data;

  }

  public void setData(String data) {

    this.data = data;

  }

  public void write() {

    System.out.println("Default Writer");

    System.out.println("Data="+data);

  }

}

 

Now change the class to JJWriter and add the property tag to your bean definition in the spring.xml file as:

<bean id="jjwriter" class="com.javajee.spring.JJWriter">

<property name = "data" value="Sample Data"/>

</bean>

 

Make sure you have a getter and setter with javabean convention as above. Without proper setters, you will get a BeanCreationException.

Now test using the same test class JJWriterMain as before and you will get the below output:

Default Writer

Data=Sample Data

 

Constructor Injection

Similar to properties you can also inject constructor arguments.

To our JJWriter class, add one more member variable retryCount of type int and a constructor that accepts both the member variables (data, retryCount). Also modify the print statement to include the new property.

  private String data;

  private int retryCount;

  

  public JJWriter(String data, int retryCount)

  {

    this.data = data;

    this.retryCount= retryCount;

  }

 

  public void write() {

    System.out.println("Default Writer");

    System.out.println("Data="+data+". retry count="+retryCount);

  }

 
Now modify the spring.xml file bean definition as:

<bean id="jjwriter" class="com.javajee.spring.JJWriter">

<constructor-arg type="java.lang.String" value="Sample Data"></constructor-arg>

<constructor-arg type="int" value="20"></constructor-arg>

</bean>

Now if you run the same test class, you will get the output as:

Data=Sample Data. retry count=20

Order of arguments are important.

The type attribute is optional if there are no conflicts, and spring will automatically convert the value to that type.

A conflict occurs when there are more constructors with same number of arguments. For instance, if we have two constructors with single arguments of String and int, and we configured a single constructor arg with a value, say "20", Spring will be confused which constructor to call as we always provide values within double quotes.

In our example, we have only one constructor with two arguments and hence we can remove the type attribute. Spring will automatically do the needful convertion and the output will be same as before.

<bean id="jjwriter" class="com.javajee.spring.JJWriter">

<constructor-arg value="Sample Data"></constructor-arg>

<constructor-arg value="20"></constructor-arg>

</bean>

You can also mix both constructor and setter injections. Now, the constructor will be called first and then setters.

<bean id="jjwriter" class="com.javajee.spring.JJWriter">

<constructor-arg value="Sample Data"></constructor-arg>

<constructor-arg value="20"></constructor-arg>

<property name="data" value="New Sample Data"></property>

</bean>

This will give the output as:

Default Writer

Data=New Sample Data. retry count=20

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!