Mastering JSR‑330 in Spring Boot 3: Real‑World Dependency Injection Cases
Explore how to leverage JSR‑330 standard annotations such as @Inject, @Named, and @ManagedBean in Spring Boot 3.4.2, with practical code examples, dependency setup, bean naming, optional injection, and a comparison of their limitations versus native Spring annotations.
1. Introduction
Dependency Injection (DI) is a key design pattern in Java enterprise development. Spring provides rich DI features, and besides its own annotations, it also supports the JSR‑330 standard annotations such as @Inject and @Named, which improve portability across IoC containers.
2. JSR‑330 Overview
JSR‑330 defines a set of annotations for DI that are framework‑agnostic. Spring has supported them since version 3.0, allowing developers to write code that can be migrated to other containers like CDI or Guice without major changes.
3. Practical Examples
3.1 Adding the Dependency
<code><dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>2.0.1</version>
</dependency></code>Spring automatically scans for @Named‑annotated classes via ClassPathBeanDefinitionScanner .
3.2 Using @Named
<code>@Named
public class PersonDAO {
// todo
}</code>The @Named annotation works like @Component; Spring registers it because ClassPathBeanDefinitionScanner includes javax.inject.Named in its default filters.
<code>@Named("personDAO")
public class PersonDAO { }</code>@ManagedBean can also be recognized, but it will be removed after Jakarta EE 11, so its use is discouraged.
3.3 Using @Inject
<code>@Named
public class PersonService {
@Inject
private PersonDAO dao;
// todo
}</code>@Inject can be placed on fields, setter methods, or constructors. It can be combined with java.util.Optional , @Nullable , or Provider<T> to handle optional or lazily‑loaded beans.
<code>// Optional injection
@Inject
private Optional<CommonDAO> commonDAO;
// Nullable injection
@Inject
@Nullable
private CommonDAO commonDAO;
// Provider injection
@Inject
private Provider<CommonDAO> commonDAO;</code>4. Limitations of JSR‑330 Compared to Spring
@Inject lacks a “required” attribute; use Optional for optional dependencies.
@Named provides only naming, without the richer qualifier model of Spring’s @Qualifier.
Scope annotations differ: Spring’s @Scope vs JSR‑330’s @Singleton; default JSR‑330 beans are singleton in Spring.
Several Spring‑specific annotations have no JSR‑330 equivalents, such as @Value, @Lazy, and ObjectFactory (replaced by Provider).
5. Conclusion
The article demonstrates how to integrate JSR‑330 annotations into Spring Boot 3 projects, offering portable DI while highlighting the functional gaps that still require Spring‑specific annotations.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.