Jakarta dependency injection and Spring autowiring
Compare Jakarta Inject with Spring autowiring, including named and typed qualifiers, lazy providers, default scopes, and optional dependencies.
A class can declare its dependencies with Jakarta
@Injector Spring’s@Autowired. Both work in Spring, and the choice affects which bean-resolution features the class can express and how easily the module can run in another container.
The examples use Jakarta Inject 2.0.1 and Spring Framework 6.x, as used by Spring Boot 3.4.
What the injection standard defines
JSR 330 was introduced in 2009 with Bob Lee and Rod Johnson as specification leads. It established a small set of dependency-injection annotations and interfaces, now maintained under Jakarta Dependency Injection. The jakarta.inject API describes injection points, scopes, and qualifiers. The container controls component discovery, instantiation, lifecycle, and dependency resolution.
That division is useful for code shared across Spring, Guice, CDI, and Dagger. Portability still depends on the container configuration around the class. A custom scope recognized in one application may need separate registration in another.
flowchart TD
APP["Application or library"] --> API["Jakarta Inject API"]
API -.-> SPRING["Spring"]
API -.-> GUICE["Guice"]
API -.-> CDI["Jakarta CDI"]
API -.-> DAGGER["Dagger"]
The six Jakarta Inject types
| Type | Purpose | Spring equivalent or neighbor |
|---|---|---|
@Inject | Marks a constructor, field, or method for injection | @Autowired |
@Named | Adds a string qualifier and can supply a discovered component name | @Qualifier or named @Component |
@Qualifier | Marks a custom annotation as a qualifier | Spring’s qualifier meta-annotation |
@Scope | Marks a custom scope annotation | Spring’s scope mechanism |
@Singleton | Declares one shared instance per injector | Singleton bean scope |
Provider<T> | Looks up a dependency at the time of get() | ObjectProvider<T> |
A provider follows the target’s configured scope. A prototype can yield a fresh instance on each lookup. A singleton stays shared. The same provider interface can therefore express both deferred initialization and access across a scope boundary.
Wiring it into Spring Boot
Add the standard API to the classpath. Spring detects jakarta.inject.Inject when it is present. The common starter does not guarantee that this API is already a dependency.
1
2
3
4
5
6
// build.gradle.kts
dependencies {
implementation("org.springframework.boot:spring-boot-starter")
// Jakarta Dependency Injection 2.0 API
implementation("jakarta.inject:jakarta.inject-api:2.0.1")
}
The payment example has an interface and a typed qualifier. Annotate the implementation and the injection point with the same qualifier. If you only have a stable bean name, @Named provides a string-based option.
1
2
3
4
5
6
// src/main/java/com/example/di/PaymentGateway.java
package com.example.di;
public interface PaymentGateway {
String pay(long amount);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
// src/main/java/com/example/di/CardPayment.java
package com.example.di;
import jakarta.inject.Qualifier;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Qualifier
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CardPayment {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// src/main/java/com/example/di/CardPaymentGateway.java
package com.example.di;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
@Named("cardPaymentGateway") // Registers a named component in Spring
@CardPayment
@Singleton
public class CardPaymentGateway implements PaymentGateway {
@Override
public String pay(long amount) {
return "CARD:" + amount;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// src/main/java/com/example/di/OrderService.java
package com.example.di;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Provider;
@Named
public class OrderService {
private final PaymentGateway paymentGateway;
private final Provider<RequestTraceContext> traceContextProvider;
@Inject
public OrderService(
@CardPayment PaymentGateway paymentGateway,
Provider<RequestTraceContext> traceContextProvider
) {
this.paymentGateway = paymentGateway;
this.traceContextProvider = traceContextProvider;
}
public String processOrder(long amount) {
// Resolve a prototype at the point of use
RequestTraceContext trace = traceContextProvider.get();
return "[" + trace.getTraceId() + "] " + paymentGateway.pay(amount);
}
}
This also shows why a singleton may inject a Provider<T> instead of storing one prototype instance. The lookup moves to the point of use. That lets the container resolve the appropriate instance for each call.
What Spring resolves during bean creation
Spring’s AutowiredAnnotationBeanPostProcessor handles @Autowired and @Value. It also recognizes Jakarta @Inject when the API is on the classpath. Spring finds the injection metadata and delegates dependency resolution to the bean factory. Candidates are selected by type and further narrowed by qualifiers and other bean-resolution rules.
flowchart TD
SCAN["Discover bean definitions"] --> BPP["AutowiredAnnotationBeanPostProcessor"]
BPP --> CHECK{"Injection annotation"}
CHECK -->|"Spring annotations"| AUTOWIRED["Spring metadata"]
CHECK -->|"Jakarta Inject"| INJECT["Jakarta metadata"]
AUTOWIRED --> RESOLVE["Bean factory resolves candidates"]
INJECT --> RESOLVE
| Decision | @Autowired | @Inject |
|---|---|---|
| API package | org.springframework.beans.factory.annotation | jakarta.inject |
| Optional dependency | required = false, Optional<T>, or a supported nullable declaration | Optional<T> or a supported nullable declaration in Spring |
| Lazy access | ObjectProvider<T> adds getIfAvailable(), ifAvailable(), and streaming | Provider<T> exposes get() |
| Default Spring scope | Singleton for a typical component | Singleton for a component discovered through @Named |
| Use across containers | Spring-specific | Standard declaration, with container-specific discovery and scope configuration |
JSR 330 has no required attribute. When the optionality is part of the type’s contract, Optional<T> puts that fact next to the constructor parameter. A Spring ObjectProvider<T> can also express absence at lookup time. A plain Jakarta Provider<T> offers no portable ifAvailable() equivalent.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// src/main/java/com/example/di/NotificationService.java
package com.example.di;
import jakarta.inject.Inject;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class NotificationService {
private final Optional<AuditLogger> auditLogger;
private final ObjectProvider<MetricsClient> metricsProvider;
// Use Optional<T> or @Nullable for optional dependencies
@Inject
public NotificationService(
Optional<AuditLogger> auditLogger,
@Nullable ObjectProvider<MetricsClient> metricsProvider
) {
this.auditLogger = auditLogger;
this.metricsProvider = metricsProvider;
}
public void notifyEvent(String message) {
auditLogger.ifPresent(logger -> logger.record(message));
// ObjectProvider can skip the action if the bean is absent
// A required Provider.get() lookup can fail when the bean is absent
if (metricsProvider != null) {
metricsProvider.ifAvailable(client -> client.increment("notify.count"));
}
}
}
Spring’s default singleton scope applies to beans it registers. CDI’s default scope can be dependent, and Guice can create unscoped instances. Declare
@Singletonin a portable module when the shared lifecycle is part of the design.
Choosing an API for each module
For an application that runs entirely on Spring Boot, I use constructor injection and omit the annotation on a single constructor. That leaves ObjectProvider<T> and the usual qualifier options available where the application needs them. For a library used in several containers, Jakarta Inject reduces compile-time coupling to Spring. In that case, I verify component discovery and scope in each target container and keep Spring-specific convenience types out of the library API.