What’s the Difference Between @Autowired and @Resource in Spring Boot?
This article explains how @Autowired and @Resource differ in Spring Boot, covering their injection mechanisms, priority rules, applicable environments, and code examples, helping developers choose the appropriate annotation based on type‑based or name‑based injection needs.
@Autowired
@Autowired is a Spring‑provided annotation that injects dependencies by type. It can be applied to constructors, fields, and methods. When multiple candidate beans exist, the specific bean can be selected with @Qualifier.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
class UserService {
public void printMessage() {
System.out.println("UserService message");
}
}
@Component
class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
public void displayMessage() {
userService.printMessage();
}
}@Resource
@Resource is a Java EE standard annotation (javax.annotation) that injects by name by default; if no matching bean name is found, it falls back to type injection. Its priority is higher than @Autowired when both are present on the same bean.
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
@Component
class UserService {
public void printMessage() {
System.out.println("UserService message");
}
}
@Component
class UserController {
@Resource
private UserService userService;
public void displayMessage() {
userService.printMessage();
}
}Differences Summary
Injection method : @Autowired injects by type and can be qualified with @Qualifier; @Resource injects by name by default and falls back to type.
Priority : @Resource has higher precedence than @Autowired when both annotations are used on the same bean.
Applicable environment : @Autowired is Spring‑specific; @Resource is a Java EE standard, making it more portable across Java EE environments.
Code brevity : @Autowired often works with @Qualifier for fine‑grained control, while @Resource usually requires only the bean name.
Usage Scenarios
Use @Autowired for complex type‑based injection in typical Spring applications.
Use @Resource when you need name‑based injection or compatibility with Java EE components.
Conclusion
Choosing between @Autowired and @Resource depends on whether you prefer type‑based injection without naming concerns (@Autowired) or name‑based injection and broader Java EE compatibility (@Resource). Understanding their differences enables more flexible dependency injection in Spring Boot projects.
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.
java1234
Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com
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.
