×
☰ See All Chapters

Setter vs. Constructor Injection

We have covered both setter injection and constructor injection, how both injection works and their configurations in our previous tutorials. Now let us see the difference between constructor and setter injection.

Setter

Constructor

Dependencies are injected using setter methods.

Dependencies are injected using constructor arguments.

Partial dependency injection is supported; we can ignore any property for injection. We can inject properties only if necessary.

Partial dependency injection is not supported; we cannot ignore the property if it is there in constructor parameter. All the parameters of constructor must be injected. Otherwise an overloaded constructor must be created with those properties need to be injected.

If we use constructor and setter injection both, setter injection will be used because setter methods will be executed after constructor.

If we use constructor and setter injection, constructor injection will not be used.

Default constructor is mandatory.

Default constructor is not mandatory.

More readable

Less Readable when there is more number of properties in class.

Less secure than constructor inject, because while using setter injection, you can override certain dependency by sub-classes overridden setter methods.

More secure than setter inject as dependency cannot be overridden, sub-classes still has to invoke super class constructors.

If two objects dependent each other, the circular dependency has no effect as objects use setter methods of other.


If two objects dependent each other, the circular dependency has an effect and exception will be thrown from spring. One object has to wait for other object creation; both the objects wait for other creation results in ObjectCurrentlyInCreationException.

setter-vs-constructor-injection-0

If setter and constructor injection both are used, then which values will be used and why?

Since methods executes after constructor execution, the values assigned through setter injection will be used as final values.

When to use Setter Injection and when to use Constructor Injection?

  • If there is only one property in a spring bean class then use constructor injection as constructor executes before methods constructor injection will be faster than setter injection. 

  • If spring bean class is having more than one property then it is better to use setter injection because it reduces burden on the programmer. Setter injection reduces burden on the programmer because programmer need not to worry about index, type of parameters as in the case of constructor injection. 

  • It is not mandatory that all properties must be configured for injection. Properties can be left with their default values. 

Constructor Injection Example

Let's see the simple example to inject primitive and object-based values. To inject primitive values we should use value attribute and to inject object values we should use ref attribute of <constructor-arg>. While injecting object values ref attribute should be assigned with id value of other beans.

Project Directory Structure

setter-vs-constructor-injection-1
 

EmplyeeAdress.java

package com.java4coding;

 

public class EmplyeeAdress {

        public String city;

        public EmplyeeAdress(String city) {

                this.city = city;

        }

}

 

EmployeeBean.java

package com.java4coding;

 

public class EmployeeBean {

        public String name;

        public EmplyeeAdress add;

        public EmployeeBean(String name, EmplyeeAdress add) {

                super();

                this.name = name;

                this.add = add;

        }

}

 

EmployeeTest.java

package com.java4coding;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class EmployeeTest {

        public static void main(String[] args) {

                BeanFactory factory = new ClassPathXmlApplicationContext("springConfig.xml");

                EmployeeBean employeeBean = (EmployeeBean) factory.getBean("studentbean");

                System.out.println(employeeBean.add.city);

                System.out.println(employeeBean.name);

        }

}

springConfig.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

 

        <bean id="adrress" class="com.java4coding.EmplyeeAdress">

                <constructor-arg name="city" value="Bangalore"></constructor-arg>

        </bean>

 

        <bean id="studentbean" class="com.java4coding.EmployeeBean">

                <constructor-arg name="add" ref="adrress"></constructor-arg>

                <constructor-arg name="name" value="Manu Manjunatha"></constructor-arg>

        </bean>

 

</beans>

Output

Bangalore

Manu Manjunatha

Setter Injection Example

Let's see the simple example to inject primitive and object-based values. To inject primitive values we should use value attribute and to inject object values we should use ref attribute of <property>. While injecting object values ref attribute should be assigned with id value of other beans.

Project Directory Structure

setter-vs-constructor-injection-2
 

EmplyeeAdress.java

package com.java4coding;

 

public class EmplyeeAdress {

        public String city;

 

        public String getCity() {

                return city;

        }

 

        public void setCity(String city) {

                this.city = city;

        }

       

}

EmployeeBean.java

package com.java4coding;

 

public class EmployeeBean {

        public String name;

        public EmplyeeAdress add;

       

        public String getName() {

                return name;

        }

        public void setName(String name) {

                this.name = name;

        }

        public EmplyeeAdress getAdd() {

                return add;

        }

        public void setAdd(EmplyeeAdress add) {

                this.add = add;

        }

}

 

 

EmployeeTest.java

package com.java4coding;

 

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class EmployeeTest {

        public static void main(String[] args) {

                BeanFactory factory = new ClassPathXmlApplicationContext("springConfig.xml");

                EmployeeBean employeeBean = (EmployeeBean) factory.getBean("studentbean");

                System.out.println(employeeBean.getAdd().getCity());

                System.out.println(employeeBean.getName());

        }

}

springConfig.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

 

        <bean id="adrress" class="com.java4coding.EmplyeeAdress">

                <property name="city" value="Bangalore"></property>

        </bean>

 

        <bean id="studentbean" class="com.java4coding.EmployeeBean">

                <property name="add" ref="adrress"></property>

                <property name="name" value="Manu Manjunatha"></property>

        </bean>

 

</beans>

 

Output

Bangalore

Manu Manjunatha

 


All Chapters
Author