Quick revision
The short answer
Setter injection is still reasonable for optional dependencies that have a sensible default and may be changed after construction, for framework classes that expose many optional properties, and, as a stopgap, for breaking certain circular dependencies, although a redesign is better. The price is that a setter-injected object can be reconfigured at any time, which is risky in a singleton shared by many threads.
What to remember
In this answer, we saw that setter injection still fits optional dependencies with sensible defaults, framework classes with many optional properties, and, as a temporary measure, breaking a circular dependency that should really be redesigned. Its cost is that the object can be reconfigured after construction, which is risky in shared singletons where other threads see the change. Required dependencies belong in the constructor; setters are for refining an object that is already valid.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Setter injection is still reasonable for optional dependencies that have a sensible default and may be changed after construction, for framework classes that expose many optional properties, and, as a stopgap, for breaking certain circular dependencies, although a redesign is better. The price is that a setter-injected object can be reconfigured at any time, which is risky in a singleton shared by many threads.
Why this question is asked
After years of hearing that constructor injection is the right way, it is fair to ask whether setters have any place left. Interviewers ask this to see whether we follow a rule because we understand it or because it is fashionable. A good answer names the specific cases where setters still fit and the risks that come with them. In this answer, we will recap what setter injection is, go through the three cases where it is still reasonable, look at the mutability risk in shared singletons, and finish with a simple way to decide.
1A Quick Recap of Setter Injection
With setter injection, Spring first creates the object with a constructor, usually the no-argument one, and then calls setter methods to hand over dependencies or settings. In Java configuration we can do it ourselves in a @Bean method, or Spring does it for setters annotated with @Autowired.
The defining property is timing. The object exists before its setters are called, so for a moment it is not fully configured, and since setters are ordinary public methods, they can also be called again later, by anyone who holds a reference.
2Case 1: Optional Dependencies With Sensible Defaults
Some collaborators are genuinely optional: the class works perfectly without them, using a default, and the dependency only replaces that default. A clock is a good example.
Notice that the object is valid before the setter is ever called, which removes the main objection to setters, the half-built object. Putting this Clock in the constructor would force every caller, including tests that do not care about time, to pass one. A constructor overload or an ObjectProvider can achieve the same, but here the setter states the intent plainly: there is a default, and you may change it.
3Case 2: Framework Classes With Many Optional Properties
Framework and infrastructure classes often have a dozen or more settings, almost all optional. Spring's own classes are full of them: ThreadPoolTaskExecutor has setCorePoolSize, setMaxPoolSize, setQueueCapacity, setThreadNamePrefix and many more.
A constructor with fifteen parameters, most of them null, would be unreadable and fragile, because positional arguments are easy to swap. Setters let us name each setting. Notice also the life-cycle hook: the class does its real set-up in afterPropertiesSet(), after all setters ran, which is how framework classes make sure they are complete before use. Modern code often prefers a builder for the same job, but setters remain common because they work with XML, with properties binding and with plain Java.
4Case 3: Breaking a Circular Dependency, and Why Redesign Is Better
If A needs B and B needs A, and both use constructor injection, Spring cannot create either one first, and startup fails with a BeanCurrentlyInCreationException. Making one side setter-injected breaks the deadlock: Spring creates A, then B with A, then calls A's setter with B. Since Spring Boot 2.6, though, circular references are refused by default, so even this needs spring.main.allow-circular-references=true, a clear sign that the framework considers it a smell.
The better fix is to remove the cycle. Usually A and B share some logic that belongs in a third class C that both depend on, or one of them only needs to notify the other, which an application event can do. I would use the setter only as a short-term patch with a comment and a ticket.
4 messages in order. Play them, or step through them one at a time.
It works, but the cycle is still there; extracting shared logic removes it.
5The Risk: Mutable Singletons
Most Spring beans are singletons, a single instance shared by every thread that serves a request. A public setter on a singleton is an invitation to change shared state at runtime. If some code calls tokenService.setClock(fixedClock) to make one test or one request behave differently, every other thread sees the change too. Without synchronisation or a volatile field, other threads may not even see the new value consistently, because the Java memory model only guarantees visibility for final fields after construction, not for later writes.
So when we do use setters on singletons, we treat them as configuration-time only: called by Spring during start-up, documented as such, and never called from business code. Some teams make the setter package-private, so only configuration in the same package can reach it.
6How to Decide
A simple test works well. If the class cannot do its job without the dependency, it goes in the constructor. If the class has a good default and the dependency only changes it, a setter or a constructor overload is fine. If there are many optional settings, setters or a builder. If we are using a setter to get around a cycle, we are paying interest on a design debt and should plan the redesign.
7Follow-up questions
Is @Autowired(required = false) on a setter still a good way to express optional?
It works and is easy to read, and if no bean exists the setter is simply not called, so the default stays. The alternatives are an ObjectProvider<Clock> constructor parameter, which keeps everything in the constructor, or an Optional<Clock> parameter. I choose the setter when a default exists in the field itself, and ObjectProvider when the dependency is used lazily or may be ambiguous.
Can setter injection and constructor injection be mixed in one class?
Yes, and this is the pattern Spring's own documentation suggests: required dependencies through the constructor, optional ones through setters. The object is then always valid after construction, and the setters only refine it.
Does setter injection make testing harder?
Not much. Tests can call the setter directly, and since there is a default, tests that do not care need not call it at all. The real test risk is shared state: a test that calls a setter on a shared bean in a Spring test context changes it for every later test that reuses the cached context.
8Conclusion
In this answer, we saw that setter injection still fits optional dependencies with sensible defaults, framework classes with many optional properties, and, as a temporary measure, breaking a circular dependency that should really be redesigned. Its cost is that the object can be reconfigured after construction, which is risky in shared singletons where other threads see the change. Required dependencies belong in the constructor; setters are for refining an object that is already valid.
Open with
I use constructor injection by default, but setter injection is still reasonable in a few specific cases. The first is an optional dependency with a sensible default, for example a Clock field initialised to Clock.systemUTC with an optional setter, so tests or special configurations can override it.
Then, if they want more
The object is valid before the setter is called, which removes the usual objection about half-built objects. The second is framework or infrastructure classes with many optional properties, like ThreadPoolTaskExecutor with core size, max size, queue capacity and thread prefix.
Fifteen positional constructor arguments would be unreadable, while named setters, followed by an afterPropertiesSet hook that finishes the set-up, are clear. The third is breaking a circular dependency between two constructor-injected beans, because Spring can create one side first and complete it with the setter.
But I treat that as a stopgap: Spring Boot refuses cycles by default since 2.6, and the real fix is usually to extract shared logic into a third bean or use an event. The main risk of setters is mutability.
Most beans are singletons shared across threads, so a public setter lets any code change shared state at runtime, and without volatile or synchronisation other threads may not even see it consistently. So I only call setters during configuration, and keep required dependencies in the constructor.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 2.6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "When is setter injection still a reasonable choice?": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/8827b6c459/