Quick revision
The short answer
DI containers became standard because they replaced hand-written singletons, factories and JNDI lookups with declarative wiring that could be swapped in tests. The classic containers did that work at runtime with reflection and classpath scanning, which costs startup time and memory. Build-time approaches such as Dagger, Micronaut, Quarkus CDI and Spring AOT move the wiring to the build for faster startup and native images, at the price of runtime flexibility, such as conditions that are fixed when we build. The principle of injecting dependencies stays; what is shifting is when the wiring is worked out.
What to remember
In this answer, we saw that DI containers replaced singletons, factories and JNDI lookups with declarative, testable wiring, which is why they became standard. Runtime containers pay for that with reflection and classpath scanning at every start. Build-time approaches, Dagger, Micronaut, Quarkus CDI and Spring AOT, move that work into the build, which helps startup time and native images, at the cost of runtime flexibility such as conditions fixed at build time. The principle of injecting dependencies stays; where and when the wiring happens is shifting.
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.
DI containers became standard because they replaced hand-written singletons, factories and JNDI lookups with declarative wiring that could be swapped in tests. The classic containers did that work at runtime with reflection and classpath scanning, which costs startup time and memory. Build-time approaches such as Dagger, Micronaut, Quarkus CDI and Spring AOT move the wiring to the build for faster startup and native images, at the price of runtime flexibility, such as conditions that are fixed when we build. The principle of injecting dependencies stays; what is shifting is when the wiring is worked out.
Background to this question
Senior engineers are expected to know not only how a tool works but why it exists and where it is going, because that is what lets them make platform decisions that last. This question tests exactly that: can we explain the problems DI containers solved, what their runtime design costs, and how the new build-time approaches change our choices for services, functions and native images. In this answer, we will walk through the problems of the pre-container era, see what containers fixed, measure what runtime wiring costs, look at how build-time DI works, and finish with how a senior person decides between them today.
1A Quick Recap: Enterprise Java Before Containers
In the early 2000s, a typical J2EE application got its objects in three ways. Singletons, classes with a static getInstance() method, held shared services. Factories created objects so callers did not need to know concrete classes. And JNDI, a directory service built into application servers, was used to look up data sources, EJBs and queues by name, as in new InitialContext().lookup("java:comp/env/jdbc/orders").
All three share the same flaw: the class reaches out to get what it needs. A singleton cannot be replaced in a test without static tricks. A JNDI lookup only works inside an application server, so a unit test needed one running. And the dependencies of a class were invisible from its API. Testing business logic was slow and painful, which is a big part of why early EJB was so disliked.
2What DI Containers Fixed
Spring in 2003-2004, and later Guice and CDI, made dependencies something a class declares and the container supplies. Configuration, first in XML and later in annotations, said which implementation goes where. That had two big effects. Business classes became plain Java objects that a unit test could construct with fakes. And cross-cutting concerns, transactions, security, remote access, could be added declaratively because the container created every object and could wrap it in a proxy.
Testability is what made DI win. Once teams could unit test services without a server, the productivity difference was obvious, and Java EE itself adopted the idea with CDI in 2009.
| Era | Code in the class | Test without a server? |
|---|---|---|
| Singleton | Db.getInstance().dataSource() | no, static state |
| JNDI lookup | ctx.lookup("java:comp/env/jdbc/orders") | no, needs a naming service |
| Runtime DI container | constructor parameter, wired at startup | yes |
| Build-time DI | constructor parameter, wiring generated at build | yes |
3What Runtime Wiring Costs
Classic containers do their work when the application starts. Spring scans the classpath for annotated classes, reads annotations through reflection, evaluates conditions such as @ConditionalOnClass for hundreds of auto-configurations, builds bean definitions, then creates beans and generates proxy classes. All of that happens on every start and needs metadata kept in memory.
For a long-running service the cost is small: a few seconds at startup and some tens of megabytes. But two trends made it matter. Containers and serverless platforms start and stop instances often, so startup time turns into latency and cost. And GraalVM native images, which compile Java ahead of time into a standalone binary, work under a closed-world assumption: every class, reflective access and proxy must be known at build time. Runtime scanning and reflection fit badly with that.
4Build-Time DI and Spring AOT
Build-time containers move the wiring work into the compiler or the build. Dagger and Micronaut use annotation processors that generate plain Java factory code at compile time, so at runtime there is no scanning and almost no reflection. Quarkus implements CDI Lite and resolves the bean graph during its build step. Spring took a related path: Spring AOT, ahead-of-time processing, runs the application's configuration during the build, evaluates conditions, and generates Java code that registers the bean definitions directly, plus hints for reflection and proxies that native images need.
- Source codeconstructor injection,
@Beanmethods, annotations - Build: annotation processor or AOT stepbuild-time DI resolves the graph and generates code here
- Startup: container refreshruntime DI scans, reflects and evaluates conditions here
- Running applicationthe same objects either way
4 layers, from the top down. Step through them one at a time.
The objects are the same; the work moves earlier.
Notice that the decision about which beans exist has already been made when this code runs. The typical result is a JVM start that is noticeably faster and a native image that starts in tens of milliseconds with much less memory. The exact numbers depend on the application, so we should measure our own.
5The Trade-Off: Flexibility Moves to the Build
What we give up is runtime flexibility. With Spring AOT, conditions such as @ConditionalOnProperty and @Profile on bean definitions are evaluated at build time, so changing a property or the active profile at startup can no longer add or remove beans that were decided during the build. Registering beans dynamically from data only known at runtime, such as a list fetched from a remote service, does not fit. Libraries that rely on reflection need hints. In return we get faster, more predictable startup and native-image support.
So the senior decision is about the workload. For a long-running service that starts rarely, runtime Spring is perfectly fine and keeps full flexibility. For functions, fast autoscaling or tight memory, AOT and native images, or a build-time framework, are worth the constraints. And in every case the design principle that made DI succeed, classes that declare their dependencies and do not fetch them, stays exactly the same. Code written that way can move between runtime and build-time wiring with little change.
6Follow-up questions
Does Spring AOT mean Spring now works like Dagger?
Partly. Both produce wiring code at build time, but Spring AOT starts from the normal Spring programming model and generates code for it, keeping most features, while Dagger is designed around compile-time generation from the start and offers fewer runtime features. Spring on the JVM can also run without AOT, which Dagger cannot.
What should teams avoid if they may adopt AOT later?
Avoid deciding which beans exist from data only known at runtime, avoid heavy custom reflection and runtime proxies built by hand, and keep conditions based on the build-time classpath or on configuration fixed per build. Check your Spring version's AOT documentation for the exact list of supported and unsupported features.
Will runtime DI containers disappear?
Unlikely. Long-running services gain little from build-time wiring, and runtime containers are more flexible and easier to debug. What is changing is that teams now have a choice per workload, and frameworks are making it easier to move the same code to build-time wiring when startup or memory matters.
7Conclusion
In this answer, we saw that DI containers replaced singletons, factories and JNDI lookups with declarative, testable wiring, which is why they became standard. Runtime containers pay for that with reflection and classpath scanning at every start. Build-time approaches, Dagger, Micronaut, Quarkus CDI and Spring AOT, move that work into the build, which helps startup time and native images, at the cost of runtime flexibility such as conditions fixed at build time. The principle of injecting dependencies stays; where and when the wiring happens is shifting.
Open with
DI containers became standard because the alternatives made testing so hard. Before Spring, services got their dependencies from singletons, factories and JNDI lookups, so a class reached out for what it needed, its dependencies were invisible, and testing often needed an application server.
Then, if they want more
Spring, and later CDI, let classes declare their dependencies and had the container supply them, which made business code plain Java that we could unit test with fakes, and let the container add transactions and security through proxies.
The cost of the classic design is that all the wiring happens at runtime: classpath scanning, reflection, evaluating hundreds of conditions on every start. That is fine for a long-running service, but it hurts with serverless, fast autoscaling and native images, which need everything known at build time.
So the newer approaches, Dagger and Micronaut with annotation processors, Quarkus resolving CDI Lite at build time, and Spring AOT generating bean registration code, move that work to the build. The trade-off is flexibility: with AOT, profiles and property conditions that decide which beans exist are fixed when we build, and dynamic registration from runtime data does not fit.
My view is that the principle has not changed at all, only where the wiring is computed, so I choose per workload and keep code in a style that can move either way.
About this answer
- Last updated
- 12 November 2026
- 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 "Why did dependency injection containers become standard in enterprise Java, and how have...": https://scrivio-interview-library.netlify.app/spring-boot/ioc-container/edd8629864/