Backend Development 8 min read

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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering JSR‑330 in Spring Boot 3: Real‑World Dependency Injection Cases

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>&lt;dependency&gt;
  &lt;groupId&gt;jakarta.inject&lt;/groupId&gt;
  &lt;artifactId&gt;jakarta.inject-api&lt;/artifactId&gt;
  &lt;version&gt;2.0.1&lt;/version&gt;
&lt;/dependency&gt;</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&lt;T&gt; to handle optional or lazily‑loaded beans.

<code>// Optional injection
@Inject
private Optional&lt;CommonDAO&gt; commonDAO;

// Nullable injection
@Inject
@Nullable
private CommonDAO commonDAO;

// Provider injection
@Inject
private Provider&lt;CommonDAO&gt; 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.

Javabackend developmentSpring Bootdependency injectionJSR-330
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.