What is the difference between Autowired and Resource keywords in Spring?

What is the difference between Autowired and Resource keywords in Spring?

@Resource and @Autowired are both used for bean injection. In fact, @Resource is not an annotation of Spring. Its package
is javax.annotation.Resource, which needs to be imported, but Spring supports injection of this annotation.
1. Common points
Both can be written on fields and setter methods. If both are written on the field, then there is no need to write setter methods.
2. Differences
(1) @Autowired
@Autowired provides annotations for Spring, which need to be imported into the package
org.springframework.beans.factory.annotation.Autowired; it is only injected according to byType.
@Autowired annotation is to assemble dependent objects according to type (byType). By default, it requires dependent objects to exist. If null values ​​are allowed, you can set its required attribute to false. If we want to use byName to assemble, we can use it in conjunction with the @Qualifier annotation.
(2) @Resource
@Resource is automatically injected according to ByName by default, provided by J2EE, and the package javax.annotation.Resource needs to be imported.
@Resource has two important attributes: name and type. Spring parses the name attribute annotated with @Resource as the name of the bean
, and the type attribute parses as the type of the bean. Therefore, if the name attribute is used, the automatic injection strategy of byName is used
, and the automatic injection strategy of byType is used when the type attribute is used. If neither the name nor the type attribute is specified, then it will pass
Use byName automatic injection strategy through reflection mechanism.
Note: It is best to put @Resource on the setter method, because this is more in line with the object-oriented thinking, to manipulate attributes through set and get instead of directly operating attributes.

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

@Resource assembly sequence:
① If both name and type are specified, the only matching bean will be found from the Spring context for assembly, and an exception will be thrown if not found.
② If the name is specified, the bean matching the name (id) is searched from the context for assembly, and an exception is thrown if it cannot be found.
③ If the type is specified, the only similar matching bean is found from the context for assembly. If it is not found or if more than one is found, an exception will be thrown.
④ If neither the name nor the type is specified, the assembly will be automatically performed in the byName method; if there is no match, it will fall back to an original type for matching, and if it matches, it will be automatically assembled.
@Resource is equivalent to @Autowired, but @Autowired is automatically injected according to byType.

Guess you like

Origin blog.csdn.net/m0_51684972/article/details/109227554
Recommended