A class that takes a required dependency as a constructor argument can only be instantiated if that argument is provided (you should have a guard clause to make sure the argument is not null) (or use a non-nullable type in Kotlin). A constructor therefore enforces the dependency requirement whether or not you're using Spring, making it container-agnostic.

If you use setter injection, the setter may or may not be called, so the instance may never be provided with its dependency. The only way to force the setter to be called is using @Required or @Autowired , which is specific to Spring and is therefore not container-agnostic.

So to keep your code independent of Spring, use constructor arguments for injection. This applies to tests; you'll have an easier time instantiating and testing the class in a normal unit test, without needing to configure an application context or the complexity that comes along with setting up an integration test.

Update: Spring 4.3 will perform implicit injection in single-constructor scenarios, making your code more independent of Spring by potentially not requiring an @Autowired annotation at all.

Answer from Emerson Farrugia on Stack Overflow
Top answer
1 of 7
100

A class that takes a required dependency as a constructor argument can only be instantiated if that argument is provided (you should have a guard clause to make sure the argument is not null) (or use a non-nullable type in Kotlin). A constructor therefore enforces the dependency requirement whether or not you're using Spring, making it container-agnostic.

If you use setter injection, the setter may or may not be called, so the instance may never be provided with its dependency. The only way to force the setter to be called is using @Required or @Autowired , which is specific to Spring and is therefore not container-agnostic.

So to keep your code independent of Spring, use constructor arguments for injection. This applies to tests; you'll have an easier time instantiating and testing the class in a normal unit test, without needing to configure an application context or the complexity that comes along with setting up an integration test.

Update: Spring 4.3 will perform implicit injection in single-constructor scenarios, making your code more independent of Spring by potentially not requiring an @Autowired annotation at all.

2 of 7
19

(...) by using Constructor Injection, you assert the requirement for the dependency in a container-agnostic manner

This mean that you can enforce requirements for all injected fields without using any container specific solution.


Setter injection example

With setter injection special spring annotation @Required is required.

@Required

Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.

Usage

import org.springframework.beans.factory.annotation.Required;

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class Foo {

    private Bar bar;

    @Inject
    @Required
    public void setBar(Bar bar) {
        this.bar = bar;
    }
}

Constructor injection example

All required fields are defined in constructor, pure Java solution.

Usage

import javax.inject.Inject;
import javax.inject.Named;

@Named
public class Foo {

    private Bar bar;

    @Inject
    public Foo(Bar bar) {
        this.bar = bar;
    }

}

Unit testing

This is especially useful in Unit Testing. Such kind of tests should be very simple and doesn't understand annotation like @Required, they generally not need a Spring for running simple unit test. When constructor is used, setup of this class for testing is much easier, there is no need to analyze how class under test is implemented.

Discussions

dependency injection - Setter DI vs. Constructor DI in Spring? - Stack Overflow
This limitation is actually an ... when setter injection is used without you even noticing. On the other hand if you use constructor injection CGLIB is not able to create a proxy, forcing you to either use interface-based proxies or a dummy no-arg constructor. See: SPR-3150 ... Sign up to request clarification or add additional context in comments. ... Thanks Tomasz.Point1:- Does spring give runtime error in case of circular dependency? Point2:-Just trying to understand with example.Are you saying ... More on stackoverflow.com
🌐 stackoverflow.com
When to use Constructor Injection vs. Setter Injection
Never use setter injection. At least, never use it as much as possible. Reason being that it encourages mutability. You should always aim for complete immutability. More on reddit.com
🌐 r/SpringBoot
11
7
March 18, 2023
Constructor injection vs Field injection
"Every time you use @Autowired on a field, a unit test dies." - Josh Long On a more serious note though, you can always make your dependencies final and use lomboks @requiredargsconstructor annotation which gives you the best from both worlds. You dont have to adjust constructor manually every time you add a new dependency, but it is still there so there is no issue with testing More on reddit.com
🌐 r/SpringBoot
20
20
July 22, 2024
Field injection is fine, convince me otherwise
Immutability. Constructor injection into private final fields makes the injected properties of your classes immutable, making runtime behaviour more predictable. **Edit there's some valid criticism of this post below, my comment is not well worded and could easily be misconstrued. More on reddit.com
🌐 r/java
211
21
September 8, 2023
🌐
Spring
spring.io › blog › 2007 › 07 › 11 › setter-injection-versus-constructor-injection-and-the-use-of-required
Setter injection versus constructor injection and the use of @Required
July 11, 2007 - Specialization in an application does not occur as often in application code as it does in framework code for example--again the number of use cases in which application code is far less. We usually advise people to use constructor injection for all mandatory collaborators and setter injection for all other properties.
🌐
Blogger
javarevisited.blogspot.com › 2012 › 11 › difference-between-setter-injection-vs-constructor-injection-spring-framework.html
Difference between Setter vs Constructor Injection in Spring
Since the setter method has name like setReporotService() by reading Spring XML config file you know which dependency you are setting. While in constructor injection, since it uses an index to inject the dependency, it's not as readable as setter injection and you need to refer either Java documentation or code to find which index corresponds to which property.
🌐
GeeksforGeeks
geeksforgeeks.org › springboot › spring-setter-injection-vs-constructor-injection
Spring - Setter Injection vs Constructor Injection - GeeksforGeeks
July 23, 2025 - A key point here is that dependencies will only be injected when the setter methods are called. Assume that all the code will remain the same as we previously wrote for constructor injections, except for a new concrete implementation Vivo and the REST Controller.
🌐
TutorialsPoint
tutorialspoint.com › difference-between-constructor-injection-and-setter-injection-in-spring
Difference Between Constructor Injection and Setter Injection in Spring
Both types of injection has their own pros and cons. Below is a list of some differences − · public class ConstructorInjectionExample { public ConstructorInjectionExample(BaseExmp baseExmp) { // ... } } <beans> <bean id = "ConstructorInjectionExample" class = "x.y.ConstructorInjectionExample"> <constructor-arg ref = "baseExmp"/> </bean> <bean id = "baseExmp" class = "x.y.BaseExmp"/> </beans> public class SetterInjectionExample { public void setBaseExmp(BaseExmp baseExmp) { this.baseExmp = baseExmp; } } <beans> <bean id = "setterInjectionExample" class = "x.y.SetterInjectionExample"> <property name = "baseExmp" ref = "baseExmp"/> </bean> </beans>
Find elsewhere
🌐
amitph
amitph.com › home › spring › spring dependency injection – field vs setter vs constructor injection
Spring Dependency Injection - Field vs Setter vs Constructor Injection | amitph
November 22, 2024 - Let’s apply the State Safety measure to all the examples we saw. Constructor Injection: State Safe. The object is instantiated to the entire state or is not instantiated at all. Setter Injection: The consumer uses a no-argument constructor.
🌐
Javatpoint
javatpoint.com › difference-between-constructor-and-setter-injection
Difference between Constructor and Setter injection in spring - javatpoint
May 1, 2013 - &gt;&gt; &lt;&lt; Setter Injection with Non-String Collection (having Dependent Object) Example Setter Injection with Non-String Collection If we have dependent object in the collection, we can inject these information by using the ref element inside the list, set or map. Here, we will use list, set or map element inside the property element. In... ... Dependency Injection by setter method Dependency Injection by constructor Injecting primitive and string-based values We can inject the dependency by setter method also.
🌐
www.java4coding.com
java4coding.com › contents › spring › setter-vs-constructor-injection
Setter vs Constructor Injection - java4coding
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.
🌐
Ask Any Difference
askanydifference.com › home › science › constructor injection vs setter injection: difference and comparison
Constructor Injection vs Setter Injection: Difference and Comparison
July 19, 2023 - Constructor injection is a design pattern in object-oriented programming where dependencies are provided to an object through its constructor, while setter injection provides the dependencies through setter methods.
🌐
Reflectoring
reflectoring.io › constructor-injection
Why You Should Use Constructor Injection in Spring
March 28, 2020 - When we have a class with multiple constructors, we need to explicitly add the @Autowired annotation to any one of the constructors so that Spring knows which constructor to use to inject the dependencies. In setter-based injection, we provide the required dependencies as field parameters to the class and the values are set using the setter methods of the properties.
🌐
Spring
docs.spring.io › spring-framework › reference › core › beans › dependencies › factory-collaborators.html
Dependency Injection :: Spring Framework
For example: Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection. If you configure beans for classes A and B to be injected into each other, the Spring IoC container detects this circular reference ...
🌐
Medium
medium.com › @nikhilsalvi011 › setter-vs-constructor-injection-c55d9434dc20
Setter vs Constructor Injection. We have covered both setter injection… | by Nikhil Sambhaji Salvi | Medium
January 29, 2024 - 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, ...
🌐
TheServerSide
theserverside.com › video › Constructor-injection-vs-setter-injection-in-Spring-Boot
Constructor injection vs. setter injection in Spring Boot | TheServerSide
Here's the short answer: Always use constructor injection in your Spring and Spring Boot applications unless a seriously compelling reason forces your hand and setter injection is a must.
🌐
Medium
saannjaay.medium.com › difference-between-constructor-and-setter-injection-in-spring-or-spring-boot-59b075a8a6b6
🚀 Constructor vs. Setter Injection in Spring Boot: Which One Should You Use? 🔥 | by Sanjay Singh | Medium
March 5, 2025 - 🚀 Constructor vs. Setter Injection in Spring Boot: Which One Should You Use? 🔥 Introduction In Spring and Spring Boot, Dependency Injection (DI) is the backbone of managing objects and their …
🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
Spring Dependency Injection – Field vs Setter vs Constructor Injection - Java Code Geeks
February 18, 2019 - Let’s apply the State Safety measure to all of the examples we saw. Constructor Injection: State Safe. The object is instantiated to a full state or is not instantiated at all. Setter Injection: Consumer uses no-argument constructor.
🌐
Java Guides
javaguides.net › 2019 › 01 › when-to-use-constructor-based-and-setter-based-di-in-spring.html
When to Use Constructor-based and Setter-based DI in Spring?
June 3, 2024 - Constructor-based DI is best for mandatory dependencies, ensuring the bean is fully initialized upon creation. Setter-based DI is suitable for optional dependencies and scenarios where you need to re-inject dependencies.
🌐
Smartprogramming
smartprogramming.in › tutorials › spring-framework › difference-between-constructor-injection-and-setter-injection-in-spring
Constructor Injection vs Setter Injection in Spring
Constructor Injection is generally better for required dependencies, as it ensures that objects are created with all necessary dependencies. This improves code reliability and makes it clear that the dependency is mandatory. Setter Injection is preferable for optional dependencies or cases ...
🌐
Java4s
java4s.com › difference between setter injection and constructor injection
Difference between Setter Injection and Constructor Injection
August 17, 2011 - Hi Yuvashree: Please look into the explanation below for both the points : 1) When you use constructor injection, the parameters you give will be assigned to that object while creating.