[Spring] Spring in @Resource and @Autowired relations

Original Address: https://www.cnblogs.com/think-in-java/p/5474740.html

Spring annotation and comparative @Resource @Autowired difference Note

Note: @Resource annotations are used to activate a named resource dependency injection (named resource) in JavaEE application, the annotations are typically converted to an object bound to the JNDI context. Spring does support the use of @Resource to resolve objects through JNDI lookup, by default, have provided notes with @Resource match the name of Spring managed object "bean name (bean name)" will be injected.
Use @Resource and @Autowired are injected bean do, in fact, not @Resource Spring annotations, its package is javax.annotation.Resource, you need to import, but injected into the Spring annotation support.

1, common

Both can be written in the field and setter methods. If both are written in the field, you do not need to write a setter method.

2, different points

(1)@Autowired

Spring @Autowired annotation is provided, the package need to import org.springframework.beans.factory.annotation.Autowired; only according byType injection.

public class TestServiceImpl {
    // 下面两种@Autowired只要使用一种即可
    @Autowired
    private UserDao userDao; // 用于字段上
    
    @Autowired
    public void setUserDao(UserDao userDao) { // 用于属性的方法上
        this.userDao = userDao;
    }
}

@Autowired annotation in accordance with the type (the byType) dependent objects assembled, by default it requires dependent objects must be present, if the null value is allowed, it may be required attribute set to false. If we want to use by name (byName) to assemble, can be combined @Qualifier annotation used together. as follows:

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

(2)@Resource

@Resource default automatic injector according ByName provided by the J2EE, need to import the package javax.annotation.Resource. @Resource has two important attributes: name and type, and Spring will @Resource annotation name attribute resolves to the bean's name, the type attribute is resolved to the type of bean. So, if you use the name attribute is used byName automatic injection strategy, and is used when using the type attribute byType automatic injection strategy. If the name is neither developed nor developing type attribute, then the policy will automatically injected using byName by reflection.

public class TestServiceImpl {
    // 下面两种@Resource只要使用一种即可
    @Resource(name="userDao")
    private UserDao userDao; // 用于字段上
    
    @Resource(name="userDao")
    public void setUserDao(UserDao userDao) { // 用于属性的setter方法上
        this.userDao = userDao;
    }
}

Note: It is best to @Resource on a setter method, because it is more consistent with object-oriented thinking through the set, get to operate the property, rather than directly to the operating property.

@Resource assembly sequence:

① If you specify the name and type, from the Spring context to find a unique match of bean assembled, can not find an exception is thrown.
② If the name is specified, from the context, find the name (id) match bean assembled, can not find an exception is thrown.
③ If the type is specified, from the context find a unique bean similar matches for assembly, can not find or find more, will throw an exception.
④ If neither specified name, and no specified type, according to the automatic assembly byName manner; if there is no match, then the match is a backoff primitive type, if a match is automatically assembled.
@Resource effect equivalent to @Autowired, but @Autowired automatically injected in accordance with byType.

Guess you like

Origin blog.csdn.net/renjingjingya0429/article/details/90232273