The @Inject annotation marks a constructor, field or method as an injection point, so the container supplies the required dependency instead of your code creating it. It comes from the Jakarta Dependency Injection specification (the jakarta.inject package), and it is supported by CDI in Jakarta EE, by Spring, by Quarkus, Guice and Dagger.
In Jakarta EE 9 and later the package is jakarta.inject.Inject. In Java EE 8 and earlier, and in the old Spring Boot 2 line, it is javax.inject.Inject. The behaviour is the same, only the package name changed.
@Inject declares a dependency. At runtime the container finds a bean whose type matches the injection point, creates or looks up an instance, and passes it in. Your class depends on an interface and never calls new on the implementation, which keeps components loosely coupled and easy to replace in tests.
The annotation has no attributes. Choosing between several candidates is done with qualifiers, which are covered below.
Constructor injection is the recommended style. Dependencies are explicit, can be final, and the object is never half-initialised.
A bean can have only one constructor annotated with @Inject. In CDI a class with a no-argument constructor needs no annotation, while Spring injects a single constructor even without @Inject.
Field injection is short, but it hides dependencies and makes plain unit tests harder because you must use reflection or a container to set the field.
Method (setter) injection is useful for optional or re-configurable dependencies. Use constructor injection by default and keep the other two for cases where they are needed.
When two beans implement the same interface the container reports an ambiguous resolution. A qualifier annotation selects one.
jakarta.inject.Named is a built-in qualifier that matches by string name. Custom qualifiers are type-safe, so a typo becomes a compile error rather than a runtime failure.
Inject a Provider<T> when you need a new instance each time or want to delay creation. CDI also offers Instance<T> for programmatic lookup of optional or multiple beans.
Spring supports @Inject when the jakarta.inject API is on the classpath. It behaves like @Autowired with required = true, so a missing bean fails at startup.
| Feature | @Inject | @Autowired |
|---|---|---|
| Origin | Jakarta Dependency Injection | Spring Framework |
| Optional dependency | Use Optional<T> or Provider<T> | required = false or Optional<T> |
| Qualifier | @Qualifier, @Named | @Qualifier, @Primary |
| Portable across containers | Yes | No |
Spring projects usually pick @Autowired or plain constructor injection. @Inject is a good fit if the same code must run on more than one container.
beans.xml or an annotation-based discovery mode).javax.inject on a Jakarta EE 10 or Spring Boot 3 runtime does not work. Migrate imports to jakarta.inject.@Inject on a new-created object: injection only happens for container-managed beans.@Inject and @Resource?@Inject resolves by type and qualifiers. @Resource resolves by name first and is aimed at container resources such as data sources.
@Inject be used on static fields?No. Injection points must be non-static, because beans are injected per instance.
@Inject replacing @EJB?In many cases yes. @Inject can inject EJBs and CDI beans, while @EJB is still valid for EJB references.
Use jakarta.inject.Inject on Jakarta EE 9 or later, Spring Framework 6 and Spring Boot 3. Use javax.inject.Inject only on older runtimes.
@Inject is the standard way to ask a container for a dependency. Prefer constructor injection, resolve ambiguity with qualifiers, use Provider for lazy or repeated lookups, and import from jakarta.inject on current platforms.