When it comes to Spring specific pros and cons:

  • Constructor injection (from the definition) does not allow you to create circular dependencies between beans. This limitation is actually an advantage of constructor injection - Spring can resolve circular dependencies 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

Answer from Tomasz Nurkiewicz on Stack Overflow
🌐
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 - The second reason why setter injection is used a lot more often than you would expect, is the fact that frameworks like Spring in general, are much more suited to be configured by setter injection than by constructor injection. This is mostly because frameworks that need to be configured often contain lots of optional values.
Discussions

java - Explain why constructor inject is better than other options - Stack Overflow
In a Pro Spring 3 Book, Chapter 4 - Introduction IOC and DI in Spring - Page 59, In "Setter Injection vs. Constructor Injection" section, a paragraph says Spring included, provide a mechanism for More on stackoverflow.com
🌐 stackoverflow.com
Choosing between field injections and constructor injections for modular Java applications with Spring Boot - Code Review Stack Exchange
Since you can mix constructor-based ... dependencies. Note that use of the @Autowired annotation on a setter method can be used to make the property be a required dependency; however, constructor injection with programmatic validation of arguments is preferable.... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
March 1, 2024
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
🌐
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.
🌐
Blogger
javarevisited.blogspot.com › 2012 › 11 › difference-between-setter-injection-vs-constructor-injection-spring-framework.html
Difference between Setter vs Constructor Injection in Spring
On the other hand, constructor injection uses the constructor to inject dependency on any Spring-managed bean. 2) Because of using the setter method, setter Injection in more readable than constructor injection in Spring configuration file usually ...
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.

🌐
TutorialsPoint
tutorialspoint.com › difference-between-constructor-injection-and-setter-injection-in-spring
Difference Between Constructor Injection and Setter Injection in Spring
Let’s say Class X is tightly dependent on Class Y then we should use constructor based injection. Setter based Injection - It can be used by calling setter methods on your beans.
Find elsewhere
🌐
Javatpoint
javatpoint.com › difference-between-constructor-and-setter-injection
Difference between Constructor and Setter injection in spring - javatpoint
May 1, 2013 - Dependency Injection by setter method Dependency Injection by constructor Injecting primitive and string-based values We can inject the dependency by setter method also. The <property> subelement of <bean> is used for setter injection. Here we are going to inject primitive and String-based values Dependent object (contained object) Collection values etc.
🌐
www.java4coding.com
java4coding.com › contents › spring › setter-vs-constructor-injection
Setter vs Constructor Injection - java4coding
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.
🌐
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.
🌐
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
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 ... 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....
🌐
Laurspilca
laurspilca.com › comparison-field-constructor-setter-injection
A comparison of field, constructor, and setter injection in Spring – LaurSpilca
annotate the constructor of the class and Spring will set the bean references in the constructor’s parameters when calling that constructor (constructor dependency injection) annotate the setter, and Spring will set the reference by calling that specific setter method (setter dependency injection).
Top answer
1 of 2
3

You can refer to the official documentation of Spring, section "Constructor-based or setter-based DI?", it says"

Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to use constructors for mandatory dependencies and setter methods or configuration methods for optional dependencies. Note that use of the @Autowired annotation on a setter method can be used to make the property be a required dependency; however, constructor injection with programmatic validation of arguments is preferable.

The Spring team generally advocates constructor injection, as it lets you implement application components as immutable objects and ensures that required dependencies are not null. Furthermore, constructor-injected components are always returned to the client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.

Setter injection should primarily only be used for optional dependencies that can be assigned reasonable default values within the class. Otherwise, not-null checks must be performed everywhere the code uses the dependency. One benefit of setter injection is that setter methods make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is therefore a compelling use case for setter injection.

Use the DI style that makes the most sense for a particular class. Sometimes, when dealing with third-party classes for which you do not have the source, the choice is made for you. For example, if a third-party class does not expose any setter methods, then constructor injection may be the only available form of DI.

2 of 2
1

First, I don't know Spring and I'm proud, so there may be some Spring-specific reasons to chose one of these styles, that I'm not aware of.
In terms of pure dependency injection however, usually constructors are a better choice, as this way your components remain more abstract from the point of view of dependency-injection framework. That is, when using field/setter injection, the framework needs to analyse in detail full structure of your components (using reflection) to find all fields/setters it needs to inject (in case of constructor injection it only needs to get the param list of a given constructor).

🌐
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 ...
🌐
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 - 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.
🌐
LinkedIn
linkedin.com › all › microservices
What are the benefits and drawbacks of using constructor injection vs setter injection in microservices?
December 21, 2023 - If a framework or tool that supports ... consideration. ... Constructor injection is favoured for mandatory dependencies, ensuring objects are fully initialised upon creation and promoting immutability....
🌐
Quora
quora.com › Which-is-better-constructor-injection-or-setter-dependency-injection
Which is better: constructor injection or setter dependency injection? - Quora
Answer (1 of 2): Constructor Injection is Better. If you want to instantiate a class you always do it with its constructor. So if you are using constructor based injection, the only way to instantiate the class is through that constructor.
🌐
Java Code Geeks
javacodegeeks.com › home › enterprise java
Spring Dependency Injection – Field vs Setter vs Constructor Injection - Java Code Geeks
February 18, 2019 - 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.