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
🌐
Medium
medium.com › spring-boot › dependency-injection-in-springboot-with-setter-and-constructor-injection-7880151b5e0f
Dependency Injection in Spring Boot with Setter and Constructor Injection | by Shagun | JavaToDev | Medium
September 13, 2024 - Setter Injection, also known as Property Injection, involves injecting dependencies into objects via setter methods. During runtime, the IoC container retrieves values from setter functions and injects them into the respective class instances.
Discussions

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
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
Spring Dependency Injection Patterns - The Good, The Bad, and The Ugly
As an added bonus, since final fields can be initialised in the constructor, our dependencies can be immutable - as they should be! Amen to that. I find stateless/immutable services to be a lot easier to deal with. Why worry about object lifetimes when you can just make every service a singleton? More on reddit.com
🌐 r/java
55
35
February 14, 2018
Setter Injection vs Constructor Injection in Spring
A pitfall of constructor injection in Spring is that CGLIB-based AOP proxies cannot be instantiated from such beans. So, for example, you cannot put @Transactional on such beans. More on reddit.com
🌐 r/java
3
6
December 4, 2012
🌐
GeeksforGeeks
geeksforgeeks.org › springboot › spring-setter-injection-vs-constructor-injection
Spring - Setter Injection vs Constructor Injection - GeeksforGeeks
July 23, 2025 - For example, let’s say we have a new user in our application, and they have their phone number as an optional field in their profile details. In this article, we have learned about Constructor and Setter injections, how to use them, and the pros and cons of each. Dependency injection is a powerful feature of Spring...
🌐
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>
🌐
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.
🌐
Spring
docs.spring.io › spring-framework › reference › core › beans › dependencies › factory-collaborators.html
Dependency Injection :: Spring Framework
Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or a no-argument static factory method to instantiate your bean. The following example shows a class that can only be dependency-injected by using pure setter injection.
Find elsewhere
🌐
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.
🌐
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.
🌐
Reflectoring
reflectoring.io › constructor-injection
Why You Should Use Constructor Injection in Spring
March 28, 2020 - In the above example, we have added the @Autowired annotation to both the setter and the field. In this case, Spring injects dependency using the setter injection method. Note that it’s bad practice to mix injection types on a single class as it makes the code less readable. Now that we have seen the different types of injection, let’s go through some of the advantages of using constructor ...
🌐
TutorialsPoint
tutorialspoint.com › spring › setter_based_dependency_injection.htm
Spring - Setter-based Dependency Injection
Inside SpellChecker constructor. Inside setSpellChecker. Inside checkSpelling. If you have many setter methods, then it is convenient to use p-namespace in the XML configuration file. Let us check the difference − · Let us consider the example of a standard XML configuration file with <property> tags − · <?xml version = "1.0" encoding = "UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id = "john-classic" class = "com.example.Person"> <property name = "name" value = "John Doe"/> <property name = "spouse" ref = "jane"/> </bean> <bean name = "jane" class = "com.example.Person"> <property name = "name" value = "John Doe"/> </bean> </beans>
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › spring-dependency-injection-by-setter-method
Spring - Dependency Injection by Setter Method - GeeksforGeeks
First, the Spring IoC container creates the object using a no-argument constructor, and then it calls the setter methods to set the required dependencies. Example: This example demonstrates how to use Setter-Based ...
Published   March 3, 2025
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.

🌐
TheServerSide
theserverside.com › video › Constructor-injection-vs-setter-injection-in-Spring-Boot
Constructor injection vs. setter injection in Spring Boot | TheServerSide
The Spring Framework allows developers to inject dependencies into their applications in one of three different ways: Field injection. Setter injection. Constructor injection. Developers typically use field injection only on small projects and prototypes, due to its inability to support immutable variables and the incompatibilities it creates with many testing frameworks.
🌐
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 - 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.
🌐
Medium
medium.com › @miguelangelperezdiaz444 › dependency-injection-in-spring-constructor-property-or-setter-which-one-should-i-choose-d38be824c8c1
💉Dependency Injection in Spring: Constructor, Property, or Setter? Discover the Best Option for Your Project! | Medium
April 5, 2023 - When Spring creates an object that has a method annotated with @Autowired, Spring looks up an instance of the corresponding dependency and assigns it to the method parameter. Like the previous one, Setter-based injection can be useful in situations where optional properties are needed or when it is difficult to modify the constructor of an existing class.
🌐
amitph
amitph.com › home › spring › spring setter dependency injection example
Spring Setter Dependency Injection Example | amitph
November 22, 2024 - Line #39: No-argument constructor of DogsController is called. Line #40: Setter of DogsController is called. The DogsService instance (created in line #37) is injected. The log shows that the objects get created first and then the dependency is injected. Very similar to Setter Injection Spring also supports Field Injection.
🌐
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.
🌐
Blogger
javarevisited.blogspot.com › 2012 › 11 › difference-between-setter-injection-vs-constructor-injection-spring-framework.html
Javarevisited: 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.