Quick revision
The short answer
With constructor injection, a class declares the dependencies it needs as constructor parameters, so they are explicit, can be final, and the object can be created with new in a plain unit test. With field injection, Spring writes the dependencies into private fields with reflection after the object exists, which hides them, prevents final fields, and makes it easy to pile up too many dependencies unnoticed. Spring recommends constructor injection as the default, needs no @Autowired on a single constructor, and keeps setter injection for optional dependencies.
What to remember
In this answer, we compared constructor and field injection. Constructor injection makes required dependencies explicit, allows final fields, lets plain unit tests create objects with new and fakes, and makes circular dependencies fail at startup. Field injection hides dependencies, needs reflection, and lets a class gather too many dependencies unnoticed. Spring uses a single constructor without @Autowired, and setter injection or ObjectProvider covers optional dependencies.
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.
With constructor injection, a class declares the dependencies it needs as constructor parameters, so they are explicit, can be final, and the object can be created with new in a plain unit test. With field injection, Spring writes the dependencies into private fields with reflection after the object exists, which hides them, prevents final fields, and makes it easy to pile up too many dependencies unnoticed. Spring recommends constructor injection as the default, needs no @Autowired on a single constructor, and keeps setter injection for optional dependencies.
Why this question is asked
Every Spring developer injects dependencies many times a day, so the style we choose shapes how testable and readable the whole code base is. Interviewers ask this question because the answer shows whether we know what Spring does when it creates a bean, not just which annotation to type. In this answer, we will recap what dependency injection means, compare field and constructor injection on the same class, see how each behaves in tests and with circular dependencies, and then look at where setter injection still fits.
1A Quick Recap of Dependency Injection
Dependency injection means a class does not create the objects it depends on; it receives them from outside. An OrderService that needs an OrderRepository and a PaymentClient gets them handed in, instead of calling new on them. In Spring, the outside is the application context, the container that creates beans and connects them.
Spring can hand in a dependency in three places: as a constructor argument when it creates the object, through a setter method after creating it, or by writing directly into a field with reflection. The last two happen after the object already exists, and that difference explains most of what follows.
2The Same Class, Two Ways
Here is a service written with field injection and then with constructor injection.
Notice three things in the second version. The dependencies are part of the constructor's signature, so anyone reading or calling it sees exactly what the class needs. The fields are final, so they cannot be reassigned or left null after construction, and final fields also get safe publication guarantees under the Java memory model. And there is no @Autowired: since Spring 4.3, a class with a single constructor has it used for injection automatically. With several constructors, we mark the one Spring should use.
3Unit Tests Without a Container
The biggest practical difference shows up in tests. With constructor injection, the class is a plain Java object, and a test creates it with new and passes fakes or mocks.
With field injection we cannot do this. new OrderService() compiles, but both fields are null, so the first call throws NullPointerException. To fill the private fields, the test needs reflection, for example Mockito's @InjectMocks, or a Spring test context that takes seconds to start. And if a dependency is added later, the field-injected test still compiles and fails at run time, while the constructor-injected test fails to compile, which tells us immediately what changed.
4Circular Dependencies Fail Early
A circular dependency is when bean A needs bean B and bean B needs bean A. With constructor injection this cannot be built: to call A's constructor, Spring needs B, and to call B's constructor it needs A. So startup fails with an error that names the cycle, and we fix the design, usually by extracting the shared part into a third bean.
With field injection, Spring could historically create A, create B, and then fill each field with the other, so the cycle was hidden and the design problem stayed. Since Spring Boot 2.6, circular references are forbidden by default even for field injection, unless a property allows them, but constructor injection makes the problem impossible to ignore in any setup.
| Concern | Field injection | Constructor injection |
|---|---|---|
| Dependencies visible in the API | No, hidden in private fields | Yes, in the constructor signature |
| final fields | Not possible | Yes |
| Plain unit test with new | Fields stay null | Works with fakes |
| Needs reflection | Yes, to write private fields | No |
| Circular dependency | Could be hidden | Fails at startup |
| Too many dependencies | Easy to miss | A long constructor is obvious |
5Spotting a Class That Does Too Much
With field injection, adding a dependency costs one line, so a class can quietly grow to twelve injected fields. A constructor with twelve parameters looks wrong immediately, in code review and in every test that has to pass them. That visible discomfort is useful: it pushes us to split the class before it becomes a god object. In this sense constructor injection does not just wire dependencies; it gives early feedback about design.
6Where Setter Injection Fits
Setter injection, with @Autowired on a setter method, still has a place for dependencies that are genuinely optional, where the class has a sensible default and the dependency may or may not be present. Spring recommends constructors for required dependencies and setters for optional ones. For an optional dependency, we can also inject an ObjectProvider<T> through the constructor and call getIfAvailable(), which keeps the field final and the class easy to test. Field injection remains acceptable in a few places where we do not construct the class ourselves, such as test classes annotated with @SpringBootTest.
7Follow-up questions
Does constructor injection work with Lombok?
Yes. @RequiredArgsConstructor generates a constructor for all final fields, and since a single constructor needs no @Autowired, Spring uses it. The code stays short and still has all the benefits of constructor injection.
How do you resolve a circular dependency that constructor injection reports?
Usually by changing the design: move the logic both beans need into a third bean, or have one side publish an event instead of calling the other directly. Marking one parameter @Lazy also breaks the cycle by injecting a proxy, but it hides the design problem, so it is a last resort.
Is field injection slower?
Not in any way that matters. The cost is in readability, testability and immutability, not run-time speed. Both are done once, when the bean is created.
8Conclusion
In this answer, we compared constructor and field injection. Constructor injection makes required dependencies explicit, allows final fields, lets plain unit tests create objects with new and fakes, and makes circular dependencies fail at startup. Field injection hides dependencies, needs reflection, and lets a class gather too many dependencies unnoticed. Spring uses a single constructor without @Autowired, and setter injection or ObjectProvider covers optional dependencies.
Open with
Constructor injection means the class declares what it needs as constructor parameters, and Spring calls that constructor with the beans. That has several advantages over field injection. The dependencies are explicit in the class's API, and the fields can be final, so the object is fully initialised and immutable after construction.
Then, if they want more
In tests I can just call new with fakes or mocks, with no Spring context and no reflection, and if someone adds a dependency the test stops compiling instead of failing with a null pointer. Circular dependencies cannot be constructed, so they fail at startup with a clear message instead of being hidden.
With field injection, Spring writes into private fields by reflection after the object exists, so the dependencies are hidden, the fields cannot be final, a plain new gives me nulls, and it's very easy to keep adding injected fields until the class does far too much, whereas a ten-parameter constructor is obviously wrong.
Since Spring 4.3, a single constructor doesn't need @Autowired. For genuinely optional dependencies I would use setter injection or inject an ObjectProvider through 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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "Explain constructor injection compared with field injection, and why constructor...": https://scrivio-interview-library.netlify.app/java/creational-patterns/07f3f4ec99/