Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Compare CDI and Spring dependency injection: annotations, default scope and how proxies are used.

CDI injects beans with @Inject, names them with @Named, chooses between candidates with qualifier annotations, and manages lifecycles with scopes such as @ApplicationScoped and @RequestScoped; its default scope is @Dependent, which ties a bean's life to the bean it is injected into, while Spring's default is singleton. For normal scopes CDI always injects a client proxy, whereas Spring injects the real bean unless we ask for a scoped proxy or an aspect applies, and Spring also accepts @Inject from jakarta.inject as an alternative to @Autowired.

Why this question is asked

Spring and Jakarta EE solve the same problem, wiring objects together without new, but with different defaults. Developers who move between a Spring Boot service and a Quarkus or Jakarta EE application are regularly surprised when a bean is created twice or a field is not the object they expected. Interviewers ask this comparison to check that we understand scopes and proxies, not just the annotations. In this answer, we will recap dependency injection, compare the annotations, look closely at the default scopes, see how each container uses proxies, and finish with how Spring supports the standard annotations.

1A Quick Recap of Dependency Injection

Dependency injection means a container creates our objects (beans) and hands each one the other beans it needs, instead of each class building its own dependencies. CDI, Contexts and Dependency Injection, is the Jakarta EE standard for this, implemented by Weld in most application servers and by ArC in Quarkus. Spring has its own container, the ApplicationContext, which predates CDI.

Two ideas matter for the comparison. A scope decides how long a bean lives and how many instances exist: one for the whole application, one per HTTP request, and so on. A proxy is a generated object that stands in for the real bean and forwards calls to it, which lets the container change which real object a call reaches.

2Annotations Side by Side

CDI marks the injection point with @Inject, gives a bean a name with @Named (mainly for EL in pages) and chooses between two implementations of the same type with a qualifier, an annotation we define ourselves and mark with @Qualifier. Scopes are annotations on the class. Spring's equivalents are @Autowired, @Component and its stereotypes (@Service, @Repository), @Qualifier with a string or a custom qualifier annotation, and @Scope or shortcut annotations such as @RequestScope.

Java
// CDI
@ApplicationScoped
public class OrderService {
@Inject @Express ShippingCalculator calculator; // custom qualifier
}
@Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE, PARAMETER})
public @interface Express {}
// Spring
@Service
public class OrderService {
private final ShippingCalculator calculator;
OrderService(@Qualifier("express") ShippingCalculator calculator) {
this.calculator = calculator;
}
}
CDI and Spring DI compared
ConcernCDISpring
Inject@Inject@Autowired, constructor, or @Inject
Declare a beanScope annotation, e.g. @ApplicationScoped@Component, @Service, @Bean
Choose between beansCustom @Qualifier annotations@Qualifier("name"), @Primary
Default scope@Dependentsingleton
Request scope@RequestScoped@RequestScope
Proxy for scoped beansAlways, for normal scopesOnly if proxyMode is set

3The Default Scope Is Different

In Spring, a bean without a scope annotation is a singleton: one instance in the whole ApplicationContext, shared by everyone who injects it. In CDI, a bean without a scope annotation is @Dependent: a new instance is created for every injection point, and it lives and dies with the bean it was injected into. If two beans inject a @Dependent PriceFormatter, each gets its own formatter.

So a class ported from Spring to CDI without adding @ApplicationScoped may suddenly be created many times. That is harmless for a stateless helper but surprising for something that holds a cache or opens a connection. In CDI, the explicit equivalent of a Spring singleton is usually @ApplicationScoped, with @Singleton as a pseudo-scope that has no proxy.

Java
@Dependent // the default if no scope is given
public class Counter { int n; }
@ApplicationScoped public class A { @Inject Counter c; }
@ApplicationScoped public class B { @Inject Counter c; }
a.c.n++; b.c.n++;
System.out.println(a.c.n + " " + b.c.n); // 1 1 (two Counter instances)
// In Spring with a plain @Component Counter, the same code prints 2 2

4How Each Container Uses Proxies

CDI divides scopes into normal scopes (@ApplicationScoped, @RequestScoped, @SessionScoped, @ConversationScoped) and pseudo-scopes (@Dependent, @Singleton). For a normal-scoped bean, CDI always injects a client proxy, never the real object. Each call on the proxy looks up the right instance for the current context, for example the bean belonging to the current request. That is why a @RequestScoped bean can be injected into an @ApplicationScoped one safely, and why CDI also creates normal-scoped beans lazily, on the first method call.

Spring injects the real bean by default. That is fine for singletons, but injecting a request-scoped bean into a singleton would capture one request's object forever, so Spring needs a scoped proxy, configured with @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS). The shortcut annotations @RequestScope and @SessionScope set that proxy mode for us. Spring also injects a proxy whenever an aspect applies to the bean, for example @Transactional, but that is an AOP proxy, a different purpose from CDI's client proxy.

Step throughA CDI client proxy picks the bean for the current request
OrderServiceClient proxyRequest contextCartBean (req 1)
cart.total()
which CartBean for this request?
instance for request 1
total()
42.50

5 messages in order. Play them, or step through them one at a time.

The application-scoped service holds only the proxy; each call reaches the current request's bean.

5Spring Supports the Standard Annotations

Spring has supported JSR-330, now Jakarta Dependency Injection, since Spring 3. With the jakarta.inject-api jar on the classpath, Spring treats @Inject like @Autowired, @Named like @Component or a qualifier, and jakarta.inject.Qualifier like its own qualifier support. Spring does not implement the CDI scopes or CDI's event and interceptor model; it only reads the basic injection annotations. Teams that want code to look portable sometimes use @Inject in Spring, but most Spring codebases prefer constructor injection without any annotation, which works the same in both.

6Follow-up questions

Does a missing scope annotation cause bugs when moving from Spring to Quarkus?

It can. Quarkus uses CDI, so a class with no scope is @Dependent, and with bean discovery that only includes annotated classes, a class with no bean-defining annotation may not be a bean at all. Adding @ApplicationScoped to service classes is the usual fix.

Why does CDI make normal-scoped beans lazy?

Because the client proxy is injected, not the real bean, the container can wait until the first method call to create the instance. Spring singletons are created eagerly at startup unless marked @Lazy, which surfaces configuration errors early but costs startup time.

Are final classes a problem for proxies?

Yes, in both. A CDI normal-scoped bean must be proxyable, so it cannot be final and needs a non-private no-argument constructor; Spring's class-based proxies have similar limits for final classes and methods. Quarkus relaxes some of these rules by generating code at build time.

7Conclusion

In this answer, we saw that CDI uses @Inject, @Named, custom qualifiers and scope annotations, while Spring uses @Autowired, stereotypes and @Qualifier but also accepts @Inject. The default scopes differ, @Dependent in CDI and singleton in Spring, which changes how many instances exist. CDI always injects a client proxy for normal scopes, whereas Spring injects the real bean unless a scoped proxy is requested or an aspect applies.

What to say in the interviewAbout 80 seconds aloud

Open with

Both are dependency injection containers, but the defaults differ. CDI marks injection points with @Inject, names beans with @Named, mostly for EL, and chooses between implementations with custom qualifier annotations.

Then, if they want more

Scopes are annotations like @ApplicationScoped, @RequestScoped and @SessionScoped. Spring uses @Autowired or plain constructor injection, @Component and its stereotypes, @Qualifier and @Primary. The big difference is the default scope. In Spring a bean without a scope is a singleton, one shared instance.

In CDI it is @Dependent, so every injection point gets its own instance tied to the life of the bean that injected it. If you port a class from Spring to CDI and forget @ApplicationScoped, you get several instances. Proxies differ too.

For normal scopes, CDI always injects a client proxy that looks up the right instance for the current context on every call, so injecting a request-scoped bean into an application-scoped one just works. Spring injects the real object unless you ask for a scoped proxy with proxyMode, which @RequestScope does for you, or an aspect like @Transactional wraps it.

And Spring supports the standard jakarta.inject annotations, so @Inject works as an alternative to @Autowired.

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
Jakarta EE specifications
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 "Compare CDI and Spring dependency injection: annotations, default scope and how proxies...": https://scrivio-interview-library.netlify.app/java/jakarta-ee-platform/a1260ebe4a/