Comparison of @Resource and @Autowired for Bean Injection in Spring

This article explains the similarities and differences between Spring's @Autowired and the JSR‑250 @Resource annotations, covering their packages, injection mechanisms, usage on fields and setters, configuration options, IDE warnings, and best‑practice recommendations for bean injection in Java backend development.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Comparison of @Resource and @Autowired for Bean Injection in Spring

@Resource and @Autowired are both used for bean injection in Spring. @Resource is not a Spring annotation; it belongs to javax.annotation.Resource and requires the corresponding import, but Spring supports its injection.

Common points

Both annotations can be placed on fields and setter methods. When placed on a field, a setter method is not required.

Differences

@Autowired

@Autowired is provided by Spring and requires importing org.springframework.beans.factory.annotation.Autowired. It performs injection by type (byType).

public class TestServiceImpl {
    // Either of the following @Autowired can be used
    @Autowired
    private UserDao userDao; // field injection

    @Autowired
    public void setUserDao(UserDao userDao) {
        // setter injection
        this.userDao = userDao;
    }
}

@Autowired injects dependencies by type and, by default, requires the dependency to be present. To allow null values, set required=false. For name‑based injection, combine it with @Qualifier:

public class TestServiceImpl {
    @Autowired
    @Qualifier("userDao")
    private UserDao userDao;
}

IDEA often shows a warning for field injection, recommending constructor‑based injection as suggested by the Spring team.

@Resource

@Resource is a JDK 1.6 annotation provided by J2EE and requires importing javax.annotation.Resource. By default it injects by name; the name attribute can specify the bean name, and it also supports injection by type.

The two important attributes of @Resource are name and type. Spring resolves name to the bean name and type to the bean class.

@Resource(name = "userDaoImpl2", type = UserDaoImpl.class)
private UserDao userDao;

If name is omitted and the annotation is on a field, the field name is used as the bean name. When placed on a setter, the property name is used. If no matching bean name is found, Spring falls back to type‑based injection, unless name is explicitly set, in which case only name‑based injection is performed.

public class TestServiceImpl {
    // Either of the following @Resource can be used
    @Resource(name="userDao")
    private UserDao userDao; // field injection

    @Resource(name="userDao")
    public void setUserDao(UserDao userDao) {
        // setter injection
        this.userDao = userDao;
    }
}
Note: It is preferable to place @Resource on setter methods to follow object‑oriented principles.

@Resource Injection Order

1. If both name and type are specified, Spring looks for a unique bean matching both; otherwise an exception is thrown.

2. If only name is specified, Spring searches for a bean with that name; failure results in an exception.

3. If only type is specified, Spring searches for a unique bean of that type; multiple or no matches cause an exception.

4. If neither name nor type is specified, Spring first attempts name‑based injection; if no match is found, it falls back to type‑based injection.

In effect, @Resource behaves similarly to @Autowired, but @Autowired defaults to by‑type injection while @Resource defaults to by‑name.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

springannotationsdependency-injection
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.