What is the difference between @Autowired, @Resource, and @Inject?
Quick Answer
@Autowired (Spring's own annotation) resolves by type first, falling back to name for disambiguation, and integrates tightly with @Qualifier/@Primary. @Resource (JSR-250, from javax/jakarta.annotation) resolves by name first, falling back to type. @Inject (JSR-330, from javax/jakarta.inject) is a standard, Spring-independent annotation that behaves similarly to @Autowired (by type, then qualifiers) but without some Spring-specific extras like the required attribute. All three are functionally similar for basic injection; the choice mostly matters for how portable the code needs to be across DI frameworks.
Detailed Answer
All three annotations trigger dependency injection, but come from different origins and resolve slightly differently:
@Autowired(Spring's own annotation,org.springframework.beans.factory.annotation): resolves by type first; if multiple candidates match, falls back to matching by the field/parameter name, or requires explicit disambiguation via@Qualifier/@Primary. Supports arequiredattribute (@Autowired(required = false)) to make a dependency optional.
@Autowired
private PaymentGateway gateway; // by type, Spring-specific
@Resource(JSR-250,javax.annotation/jakarta.annotation— a Java EE/Jakarta EE standard, not Spring-specific): resolves by name first (matching the field/parameter name against a bean name), falling back to by type if no name match is found.
@Resource(name = "stripeGateway")
private PaymentGateway gateway; // by name first — standard, not Spring-only
@Inject(JSR-330,javax.inject/jakarta.inject— a broader dependency-injection standard also supported by other DI frameworks like Guice): behaves very similarly to@Autowired— by type, then qualifiers (@Namedis JSR-330's equivalent of@Qualifier) — but has norequiredattribute or other Spring-specific conveniences, and no direct equivalent to@Autowired's constructor-optional inference.
Practical guidance: @Autowired is by far the most common choice in Spring codebases and integrates most smoothly with Spring-specific features (@Qualifier, @Primary, required = false). @Inject/@Resource matter mainly when writing code intended to be portable across DI frameworks, or in codebases with a standards-first convention — functionally, for the common case of a single unambiguous dependency, all three behave the same.