Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Why is constructor injection recommended over field injection in Spring? Is @Autowired needed on the constructor?

Constructor injection is recommended because it lets dependencies be final fields, so the object is fully built and valid the moment its constructor returns, and because the constructor signature shows exactly what the class needs, which lets tests create it with new. Field injection hides dependencies, needs reflection in tests and allows half-built objects. Since Spring 4.3, a class with a single constructor does not need @Autowired on it.

Why this question is asked

Almost every Spring code review eventually has a comment about @Autowired on a field. Interviewers ask this question because the answer reveals whether we understand what dependency injection is for, not just how to make Spring fill in a field. In this answer, we will recap the three injection styles, compare field and constructor injection on the same class, see what changes in tests and in object safety, look at when @Autowired is still needed, and finish with the design signal that a long constructor sends.

1A Quick Recap of Injection Styles

Dependency injection means a class receives the objects it works with, its dependencies, instead of creating them itself. Spring can hand those dependencies over in three ways: through constructor parameters, through setter methods, or by writing directly into fields, which it does with reflection, Java's ability to read and change fields at runtime even when they are private.

All three produce a working application. The differences show up in how safe the object is, how readable the class is, and how easy it is to test without Spring.

2The Same Class, Two Ways

Here is a service written with field injection, and then with constructor injection.

Java
// Field injection
@Service
class InvoiceService {
@Autowired private CustomerRepository customers;
@Autowired private TaxCalculator tax;
}
// Constructor injection
@Service
class InvoiceService {
private final CustomerRepository customers;
private final TaxCalculator tax;
InvoiceService(CustomerRepository customers, TaxCalculator tax) { // no @Autowired needed
this.customers = customers;
this.tax = tax;
}
}

The second version is a few lines longer, but notice what we gained. The fields are final, so the compiler guarantees they are set exactly once and never changed. And the constructor is the single place that lists the dependencies; anyone reading the class sees them immediately.

3Why Fully Built Objects Matter

With field injection, Spring first calls the no-argument constructor and only afterwards fills in the fields. Between those two moments the object exists with null dependencies. Usually nobody sees that window, but sometimes code does: a constructor that calls a method using customers fails with a NullPointerException, and a bean that someone creates with new outside Spring stays half built forever, failing only when a method is first called.

With constructor injection there is no such window. The object cannot exist without its dependencies, because the only way to create it is to pass them in. If Spring cannot find a TaxCalculator, startup fails immediately with a clear message pointing at the constructor parameter, instead of failing later on the first request. Final fields also give a guarantee from the Java memory model: once the constructor finishes, every thread that sees the object sees those fields set, which matters for singletons shared across request threads.

4What Changes in Tests

The difference is clearest in a unit test. With constructor injection, the test just calls the constructor.

Java
// Constructor injection: plain Java, no Spring, no reflection
InvoiceService service = new InvoiceService(new InMemoryCustomers(), new FixedTax("0.20"));
assertEquals(new BigDecimal("120.00"), service.total("c-1", new BigDecimal("100.00"))); // passes
// Field injection: new compiles, but the dependencies are null
InvoiceService broken = new InvoiceService();
broken.total("c-1", new BigDecimal("100.00")); // NullPointerException
// so tests need ReflectionTestUtils.setField(...) or @InjectMocks to reach private fields
Constructor versus field injection
QuestionConstructorField
Can fields be final?yesno
Valid right after new?yes, alwaysno, null until Spring fills it
Dependencies visible?in the constructor signaturescattered over the fields
Unit test without Spring?call new with fakesneeds reflection or @InjectMocks
Missing dependency foundat startup, at the parameterat startup, or later outside Spring

With field injection, the compiler lets us create the object without its dependencies, and the test must use reflection to push them into private fields, either with ReflectionTestUtils or by letting Mockito's @InjectMocks do it silently. If someone adds a third dependency, the constructor-injected test stops compiling and points at the problem; the field-injected test still compiles and fails with a NullPointerException somewhere inside the method.

5Is @Autowired Needed on the Constructor?

Since Spring Framework 4.3, if a class has exactly one constructor, Spring uses it for injection automatically, so @Autowired adds nothing. That is why modern Spring code rarely shows the annotation at all, and why a class can be free of Spring imports apart from @Service.

We still need @Autowired when a class has more than one constructor and we want Spring to use a particular one, because Spring cannot guess. Without it, Spring looks for a no-argument constructor and fails with an error such as No default constructor found if there is none. In practice the better fix is usually to keep a single constructor.

6What a Long Constructor Tells Us

Constructor injection has one effect that people sometimes complain about: a class with nine dependencies gets a constructor with nine parameters, which looks ugly. That ugliness is useful. Field injection made it painless to add a tenth @Autowired line, so classes quietly grew until they did five jobs. A long constructor makes the problem visible. When we see more than about five or six parameters, it is usually time to ask whether the class has too many responsibilities and should be split, for example into an InvoiceCalculator and an InvoiceNotifier.

7Follow-up questions

Does constructor injection prevent circular dependencies?

It does not prevent them, but it exposes them. If A needs B in its constructor and B needs A in its constructor, Spring cannot create either and fails at startup with a BeanCurrentlyInCreationException. Field injection used to hide such cycles, and since Spring Boot 2.6 circular references are refused by default anyway. The real fix is to redesign, for example by extracting the shared logic into a third bean.

Can we use records as Spring beans with constructor injection?

Yes. A record has a canonical constructor with all its components, so Spring injects the dependencies through it, and the fields are final by design. It works well for small configuration-like beans, but records are meant as data carriers, so for services with behaviour a normal class with final fields usually reads better.

Is field injection ever acceptable?

In test classes it is common and harmless: @Autowired fields in a @SpringBootTest class, because the test class is created by the test framework and never constructed by hand. In production code, I would only accept it in legacy code that we are not touching, and convert it when we change the class.

8Conclusion

In this answer, we saw that constructor injection is recommended because dependencies become final fields, the object is complete as soon as it is constructed, and the constructor signature documents what the class needs, so tests can create it with new and fakes. Field injection hides dependencies, leaves a window where fields are null, and needs reflection in tests. Since Spring 4.3, a single constructor needs no @Autowired, and a constructor with many parameters is a useful warning that the class does too much.

What to say in the interviewAbout 90 seconds aloud

Open with

Constructor injection is recommended because it produces objects that are complete the moment they exist. The dependencies are constructor parameters assigned to final fields, so the compiler guarantees they are set once and never changed, the object can never be seen with a null dependency, and final fields are safely published to other threads, which matters for singletons.

Then, if they want more

It also makes the class honest: the constructor signature is a list of exactly what the class needs.

That pays off in tests, because I can write new InvoiceService with an in-memory repository and a fake tax calculator, with no Spring context and no reflection, and if someone adds a dependency the test stops compiling instead of failing with a NullPointerException.

Field injection does the opposite: Spring calls the no-argument constructor and then writes into private fields with reflection, so dependencies are hidden, the object is half built in between, and tests need ReflectionTestUtils or Mockito's InjectMocks. On the annotation: since Spring 4.3, if a class has exactly one constructor, Spring uses it automatically, so @Autowired is not needed.

I only add it when there are several constructors and I must choose one. Finally, a constructor with many parameters is a useful signal that the class has too many responsibilities and should probably be split.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 2.6, Spring Framework 4
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.
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 "Why is constructor injection recommended over field injection in Spring? Is @Autowired...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/5ab4d1aa0b/

More in Dependency injection