When to Use @Autowired vs @Resource in Spring? A Deep Dive
This article explains the differences between Spring's @Autowired and JSR‑250's @Resource annotations, covering their injection strategies, supported features, usage examples, comparison tables, and performance considerations for backend developers.
Interview Answer
@Autowiredis a Spring-provided annotation that injects dependencies by type (byType) by default and can be combined with @Qualifier to specify a name. @Resource is defined by the JDK (JSR‑250) and injects by name (byName) by default, falling back to type if no matching name is found.
The main differences are the source (Spring vs JSR‑250), injection order (type‑first vs name‑first), and supported features (e.g., @Autowired supports constructors, methods, fields, parameters, generics, @Primary, @Qualifier, required=false, etc.).
1. Basic Concepts
@Autowired
Source: Spring framework specific annotation, not part of Java EE.
Package: org.springframework.beans.factory.annotation.Autowired Purpose: Spring container creates beans and manages their dependencies.
@Resource
Source: Part of JSR‑250, a Java EE standard annotation.
Package: javax.annotation.Resource (Java EE 8 and earlier) or jakarta.annotation.Resource (Java EE 9+).
Purpose: Java EE container injects the corresponding resource.
// @Autowired example
import org.springframework.beans.factory.annotation.Autowired;
public class UserService {
@Autowired
private UserDao userDao;
}
// @Resource example
import javax.annotation.Resource;
public class UserService {
@Resource
private UserDao userDao;
}2. Differences in Injection Mode and Order
@Autowired Injection Order
Defaults to type‑based injection; throws an exception if multiple beans of the same type exist.
Can be combined with @Qualifier to perform name‑based injection.
Supports required=false to make the dependency optional.
@Service
public class UserService {
// Type injection; error if multiple implementations
@Autowired
private UserDao userDao;
// Name injection with @Qualifier
@Autowired
@Qualifier("myUserDao")
private UserDao specificUserDao;
// Optional dependency
@Autowired(required = false)
private LogService logService;
}@Resource Injection Order
Defaults to name‑based injection (field name used as bean name).
If no bean matches the name, falls back to type‑based injection.
Can explicitly specify name or type attributes.
@Service
public class UserService {
// Name injection using field name "userDao"
@Resource
private UserDao userDao;
// Explicit name
@Resource(name = "myUserDao")
private UserDao specificUserDao;
// Explicit type
@Resource(type = LogService.class)
private LogService logService;
}Feature Comparison
Field injection: both support.
Setter injection: both support.
Constructor injection: supported by @Autowired only.
Method‑parameter injection: supported by @Autowired only.
Collection injection (List, Map, etc.): both support (limited for @Resource).
Generic injection: supported by @Autowired only.
@Primary support: @Autowired only.
@Qualifier support: @Autowired only (name attribute for @Resource). required=false support: @Autowired only.
@Order support: @Autowired only.
Usable outside Java EE: both, but @Resource needs extra dependency.
Complies with Java EE standard: @Resource yes, @Autowired no.
3. Injection Process Comparison
4. Performance and Internal Implementation
@Autowired
Processed by Spring's AutowiredAnnotationBeanPostProcessor.
Integrates with Spring extension points and lifecycle.
Deeply tied to Spring's dependency‑injection mechanism.
@Resource
Processed by Spring's CommonAnnotationBeanPostProcessor.
Implements the JSR‑250 specification.
Integrated into Spring via an adapter pattern.
From a performance perspective, the difference between the two is minimal.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Xuanwu Backend Tech Stack
Primarily covers fundamental Java concepts, mainstream frameworks, deep dives into underlying principles, and JVM internals.
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.
