When to Choose @Autowired vs @Resource in Spring? A Deep Dive
This article compares Spring's @Autowired and JDK's @Resource annotations, detailing their injection mechanisms, parameter differences, default behaviors, and usage scopes with code examples, while also offering interview advice and promoting a Java interview guide.
@Autowired is a Spring-provided annotation that can be placed on constructors, fields, and method parameters to enable automatic dependency injection based on type.
public class Service {
// Constructor injection
@Autowired
public Service(Service service) {
this.service = service;
}
// Field injection
@Autowired
private Service service;
// Setter injection
@Autowired
public void setService(Service service) {
this.service = service;
}
}@Resource is a JDK annotation defined by JSR‑250, available since JDK 1.6. It works in any Java framework and supports both type‑based and name‑based injection.
public class Service {
@Resource(name = "service1")
private Service service1;
@Resource(name = "service2")
private Service service2;
}The two annotations differ in several aspects:
Annotation parameters: @Autowired has a single required attribute (default true); @Resource defines multiple attributes such as name, type, lookup, authenticationType, etc.
Default injection mode: @Autowired defaults to by‑type; @Resource defaults to by‑name.
Applicable targets: @Autowired can be used on constructors, fields, method parameters, and other annotations; @Resource can be used on classes, fields, and method parameters.
Origin: @Autowired is defined by Spring; @Resource follows the JSR‑250 specification and is part of the JDK.
Resolution order: @Autowired first attempts by‑type, then by‑name; @Resource follows a flow illustrated in the following diagram.
public @interface Autowired {
boolean required() default true;
} public @interface Resource {
String name() default "";
String lookup() default "";
Class<?> type() default java.lang.Object.class;
enum AuthenticationType { CONTAINER, APPLICATION }
AuthenticationType authenticationType() default AuthenticationType.CONTAINER;
boolean shareable() default true;
String mappedName() default "";
String description() default "";
}Injection order for @Autowired:
Injection order for @Resource varies depending on whether name and/or type are specified, as shown in the following diagrams.
A summary table comparing the two annotations is provided below.
Interview tip
When asked about the differences between @Autowired and @Resource, explain the five points above and emphasize that @Autowired is Spring‑specific while @Resource is framework‑agnostic, making the former preferable in pure Spring 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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
