Spring Difference between Setter and Constructor Injection

Spring supports two types of injection Setter Injection which uses POJO’s(getters and setters) for injecting and the other type is Constructor Injection which uses constructors for injection. Both approach of injection has their own pros and cons lets now discuss about them in this article.

1. Partial Injection

In Setter injection partial injection is possible, Suppose if we have 3 dependencies like int,float and long. If we have not injected the values for any of the primitive then it will take default value for it. In constructor injection it is not possible as we cannot call a constructor without the matching parameters. Lets look into the below example

public class Test 
{
    private int val1;
    private float val2;
    private long val3;
    public int getVal1() {
        return val1;
    }
    public void setVal1(int val1) {
        this.val1 = val1;
    }
    public float getVal2() {
        return val2;
    }
    public void setVal2(float val2) {
        this.val2 = val2;
    }
    public long getVal3() {
        return val3;
    }
    public void setVal3(long val3) {
        this.val3 = val3;
    }
}

We have Test class with a int,float and long primitives and their corresponding POJO’s
In our configuration file we will inject partially.

 <bean id="test" class="com.javainterviewpoint.Test">
      <property name="val1" value="10"></property>
 </bean>

On running our main class

        Test test = (Test)bf.getBean("test");
        System.out.println(test.getVal1());
        System.out.println(test.getVal2());
        System.out.println(test.getVal3());

We get the default value assigned for the primitives which is not injected.

10
0.0
0

2. More number of dependencies

If we have more number of dependencies in your class then Setter Injection is not advisable. Lets say if we have 15 properties then we need to write 15 getters and setters which will increase the size of the bean. In those cases Constructor injection is the best option.

3. Setter Injection overrides Constructor Injection

If we have setter for a property and we are injecting value to it by Setter and Constructor injection then Setter injection will override the value injected through Constructor injection. We will modify our Test class a little bit and add a constructor to it

public class Test 
{
    private int val1;
    
    public Test(int val1)
    {
        this.val1=val1;
    }
    
    public int getVal1() {
        return val1;
    }
    public void setVal1(int val1) {
        this.val1 = val1;
    }
}

Add <constructor-arg> tag to our configuration file.

 <bean id="test" class="com.javainterviewpoint.Test">
     <property name="val1" value="10"></property>
     <constructor-arg value="111"></constructor-arg>
 </bean>

Even though we have set the value to val1 property as ‘111’ through constructor, it will be overridden by setter injection, we will get the final output as ’10’ only.

Leave a Reply

Your email address will not be published. Required fields are marked *