Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Spring's recommended injection style changed from XML setter injection to field injection with annotations to constructor injection. What drove these changes and what does it teach about framework conventions?

Spring moved from XML setter injection, which kept classes free of the framework but put verbose wiring far from the code, to annotation-based field injection, which made wiring short but tied classes to the container and hid their dependencies, and then to constructor injection with implicit autowiring, which keeps configuration short and classes plain and testable. Language features such as final fields and records, and the rise of unit testing, shaped each step. The lesson is to prefer conventions that keep business code independent of the framework.

Why this question is asked

Senior engineers are asked about history not to test memory, but to test judgment. Every framework convention we follow today was once a new idea that replaced an older one, and some of today's conventions will be replaced too. Interviewers ask this question to see whether we understand the forces behind those changes, so we can evaluate the next convention on its merits. In this answer, we will walk through the three eras of Spring injection, what each solved and what each broke, the outside forces that drove the changes, and the lessons a technical leader can draw for choosing and setting conventions.

1Era 1: XML and Setter Injection

In the early 2000s, Spring was a reaction against heavyweight Java EE components that had to extend framework classes and could only run inside a container. Spring's promise was the plain Java object: business classes with no framework imports at all, wired together by an external XML file.

XML
<!-- applicationContext.xml, around 2005 -->
<bean id="orderService" class="com.shop.OrderService">
<property name="repository" ref="orderRepository"/> <!-- calls setRepository(...) -->
<property name="payments" ref="paymentClient"/>
</bean>

Setter injection dominated, partly because JavaBeans conventions, a no-argument constructor plus getters and setters, were what tools of the time understood, and partly because XML constructor-arg elements were awkward with many positional arguments. The classes stayed beautifully independent of Spring. The cost was that wiring lived in a separate file, often thousands of lines long, that drifted from the code, broke on renames only at runtime, and that few developers enjoyed reading.

2Era 2: Annotations and Field Injection

Java 5 brought annotations, and Spring 2.5 in 2007 added @Autowired and component scanning. Wiring moved next to the code and shrank to a single line per dependency. Putting @Autowired directly on a private field was the shortest form of all, so it spread quickly through tutorials and code bases.

It was a big step forward in productivity, but it quietly gave up the original promise. A class with @Autowired private fields depends on the container: it cannot be correctly built with new, its dependencies are hidden from its signature, and tests need reflection or a Spring context to fill the fields. It also made it effortless to add the tenth dependency, so classes grew large without anyone feeling it.

3Era 3: Constructor Injection With Implicit Autowiring

Spring Framework 4.3 in 2016 made a small change with a big effect: a class with a single constructor no longer needed @Autowired on it. Constructor injection suddenly cost no annotation at all, and the Spring team began recommending it as the default.

Java
@Service
class OrderService {
private final OrderRepository repository;
private final PaymentClient payments;
OrderService(OrderRepository repository, PaymentClient payments) { // no Spring import needed here
this.repository = repository;
this.payments = payments;
}
}
Three eras of Spring injection
EraGainedLost
XML + setters (2004)classes free of Springwiring verbose and far from code
Annotations + fields (2007)short wiring next to codehidden deps, container-bound classes
Constructors, implicit (2016)short wiring and plain classeslong constructors are visible
Each step kept what the last one gained and repaired what it lost.

Notice that this combines the best of the two previous eras. Configuration stays short, as with annotations, because scanning and type-based wiring do the work. The class is a plain Java object again, as with XML: it can be created with new, its fields are final, and its dependencies are in its signature.

4The Forces Behind the Changes

None of these shifts happened because the framework authors changed their taste. Language features moved first. Annotations made in-code wiring possible; later, a culture of immutability, final fields, and records in Java 16 made objects that are complete at construction the norm, and constructors are the natural way to build such objects. Lombok's @RequiredArgsConstructor removed the boilerplate argument before 4.3 even arrived.

Testing practice moved too. As unit testing and test-driven development became standard, the pain of field injection became daily pain: every test needed Mockito's @InjectMocks or reflection. Constructor injection makes the test as simple as calling new. Finally, tooling and scale mattered: IDEs could now navigate annotation-based wiring, Spring Boot's auto-configuration removed most explicit configuration, and later AOT and native images rewarded designs where dependencies are explicit and known at build time.

5Lessons for Choosing Conventions

The first lesson is the one the history keeps returning to: prefer conventions that keep business code independent of the framework. The XML era had it, field injection lost it, and constructor injection regained it. Code that is plain Java survives framework upgrades, framework changes and new runtimes better than code that depends on container magic.

The second is that convenience conventions should be judged by their long-term cost, not their line count. Field injection saved a few lines per class and cost readability and testability for years. When a team proposes a new shortcut, I ask what it hides and who pays for that later.

The third is about how to change a convention at scale. When I lead a code base through a change like this, I do not ask for a big-bang rewrite. We write the new rule down with its reasons, enforce it for new and changed code with a review checklist or a static check such as an ArchUnit rule or a Sonar rule that flags @Autowired fields, and convert old classes when we touch them. Within a year or two most active code follows the new convention, and the untouched code is, by definition, code nobody needs to change.

Step throughIntroducing a convention change in a large code base
  1. Write the rule and its reasonsshort decision record
  2. Enforce on new and changed codereview checklist, ArchUnit or Sonar rule
  3. Convert old classes when touchedno big-bang rewrite
  4. Measure adoption, then tightenfail the build once old usages are rare

4 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Was field injection a mistake?

It was a reasonable trade-off at the time: it made Spring much easier to adopt, and the costs only became obvious as testing and code size grew. The mistake was continuing it after better options existed. That is the general pattern: conventions are right for a context, and the context changes.

What convention do you think will change next?

I would expect more of the wiring to move to build time, because AOT and native images reward explicit, statically known configuration. Conventions that rely on runtime reflection or classpath scanning may give way to generated or programmatic registration. The same lesson applies: the more our business code is plain Java, the less such a shift costs us.

How do you convince a team to change a long-standing habit?

With a concrete example from their own code: a test that needed reflection, a bug from a null field, or a class whose twelve @Autowired fields hid that it did four jobs. Then a small, cheap rule for new code. People accept a change when they see their own pain in it and the cost of adopting it is low.

7Conclusion

In this answer, we followed Spring's injection style through three eras. XML setter injection kept classes free of Spring but put verbose wiring far from the code; annotation-based field injection made wiring short but tied classes to the container and hid dependencies; constructor injection with implicit autowiring since Spring 4.3 combines short configuration with plain, testable classes. Annotations, final fields, records, testing culture and later AOT drove each change. The lasting lesson is to prefer conventions that keep business code independent of the framework, and to change conventions gradually with clear reasons.

What to say in the interviewAbout 100 seconds aloud

Open with

I see three eras, each driven by outside forces. In the XML era, Spring's promise was plain Java objects with no framework imports, wired by an external XML file.

Then, if they want more

Setter injection fit the JavaBeans conventions of the time, and classes were completely independent of Spring, but the wiring was verbose, lived far from the code and broke on renames at runtime. Java 5 annotations and Spring 2.5 brought @Autowired and component scanning, and field injection became popular because it was the shortest.

It moved wiring next to the code, but it quietly gave up the original promise: classes could not be built correctly with new, dependencies were hidden, and tests needed reflection. Then Spring 4.3 made a single constructor autowire without any annotation, and constructor injection became the recommendation.

It combines short configuration with plain classes that have final fields and can be tested by calling new. The forces were language features like annotations, final fields and later records, a testing culture that made field injection painful daily, and later AOT, which rewards explicit dependencies.

The lesson I take is to prefer conventions that keep business code independent of the framework, and to judge shortcuts by their long-term cost.

When I change a convention as a leader, I write down the reason, enforce it on new code with a review rule or an ArchUnit check, and convert old classes when we touch them, instead of a big-bang rewrite.

About this answer

Last updated
12 November 2026
Versions it names
Java 16, Java 5, 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 "Spring's recommended injection style changed from XML setter injection to field...": https://scrivio-interview-library.netlify.app/spring-boot/dependency-injection/6ebae4b8be/