When to Use @Autowired vs @Resource in Spring? 5 Key Differences Explained
This article compares Spring’s @Autowired and Java’s @Resource annotations, detailing five key differences—including their origins, lookup order, supported parameters, injection styles, and IDE warnings—while providing code examples and diagrams to help developers choose the appropriate annotation.
When developing with IDEA, you may notice a warning on fields annotated with @Autowired while @Resource shows no warning. This article explains the five main differences between these two Spring/Spring Boot dependency‑injection annotations.
Different origin: @Autowired is defined by Spring, @Resource comes from the Java JSR‑250 specification.
Dependency lookup order differs: @Autowired searches by type first, then by name; @Resource searches by name first, then by type.
Supported parameters differ: @Autowired supports only the required attribute, while @Resource supports seven attributes (name, type, etc.).
Injection style support differs: @Autowired supports field, constructor, and setter injection; @Resource supports only field and setter injection.
IDE warnings differ: IDEA may flag @Autowired on Mapper beans as an error, whereas @Resource does not produce the warning.
1. Different Origin
@Autowired is a Spring‑defined annotation; @Resource is a Java‑defined annotation from JSR‑250 (Java Specification Requests).
JSR stands for Java Specification Requests, which are proposals for Java standards. Only finalized JSRs are published with numbers like JSR‑250.
2. Different Dependency Lookup Order
Both annotations perform name and type lookup, but the order is opposite.
2.1 @Autowired Lookup Order
@Autowired first looks up by type; if multiple beans match, it falls back to name lookup.
The process can be seen in Spring’s source class
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#postProcessPropertyValues.
2.2 @Resource Lookup Order
@Resource first looks up by name; if not found, it falls back to type lookup.
The implementation resides in
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#postProcessPropertyValues.
2.3 Lookup Order Summary
@Autowired: type first, then name if multiple beans exist.
@Resource: name first, then type if name not found.
3. Different Supported Parameters
Both annotations can specify parameters, but the number and names differ.
@Resource(name = "userinfo", type = UserInfo.class)
private UserInfo user;@Autowired only supports a single required attribute, while @Resource supports seven attributes.
4. Different Injection Support
The three common injection styles are field injection, constructor injection, and setter injection.
Field injection
Constructor injection
Setter injection
Code examples:
a) Field injection
@RestController
public class UserController {
// field injection
@Autowired
private UserService userService;
@RequestMapping("/add")
public UserInfo add(String username, String password) {
return userService.add(username, password);
}
}b) Constructor injection
@RestController
public class UserController {
// constructor injection
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping("/add")
public UserInfo add(String username, String password) {
return userService.add(username, password);
}
}c) Setter injection
@RestController
public class UserController {
// setter injection
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping("/add")
public UserInfo add(String username, String password) {
return userService.add(username, password);
}
}@Autowired supports all three injection styles, while @Resource supports only field and setter injection.
5. Different IDE Warnings
When using IDEA Professional to inject a Mapper bean, @Autowired triggers a warning, though the program runs correctly.
Switching the annotation to @Resource removes the warning.
Conclusion
Origin: @Autowired is from Spring; @Resource is from JSR‑250.
Lookup order: @Autowired – type then name; @Resource – name then type.
Parameters: @Autowired supports one required attribute; @Resource supports seven attributes.
Injection styles: @Autowired supports field, constructor, and setter; @Resource supports field and setter only.
IDE warnings: @Autowired may cause IDEA warnings for Mapper beans, while @Resource does not.
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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
