Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Mastering JSR‑330 in Spring Boot 3: Real‑World Dependency Injection Cases

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

<dependency>
  <groupId>jakarta.inject</groupId>
  <artifactId>jakarta.inject-api</artifactId>
  <version>2.0.1</version>
</dependency>

Spring automatically scans for @Named‑annotated classes via ClassPathBeanDefinitionScanner.

3.2 Using @Named

@Named
public class PersonDAO {
    // todo
}

The @Named annotation works like @Component; Spring registers it because ClassPathBeanDefinitionScanner includes javax.inject.Named in its default filters.

@Named("personDAO")
public class PersonDAO { }

@ManagedBean can also be recognized, but it will be removed after Jakarta EE 11, so its use is discouraged.

3.3 Using @Inject

@Named
public class PersonService {
    @Inject
    private PersonDAO dao;
    // todo
}

@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.

// Optional injection
@Inject
private Optional<CommonDAO> commonDAO;

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

// Provider injection
@Inject
private Provider<CommonDAO> commonDAO;

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentSpring BootDependency InjectionJSR-330

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion