[Spring Notes] The difference between @Autowired and Resource keywords

In the usage scenario, @Autowired is used customarily. If the interface has multiple implementation services, @Resource is usually used.
but other things

Both @Resource and @Autowired are used for bean injection. In fact, @Resource is not a Spring annotation. Its
package is javax.annotation.Resource, which needs to be imported, but Spring supports the 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 the setter method.

2. Differences
(1) @Autowired
@Autowired provides annotations for Spring, you need to import the package
org.springframework.beans.factory.annotation.Autowired; only inject byType.

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

The @Autowired annotation assembles dependent objects according to type (byType). By default, it requires that dependent objects must exist. If
null values ​​are allowed, you can set its required attribute to false. If we want to use assembly by name (byName)
, we can use it in conjunction with the @Qualifier annotation. as follows:

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

(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, and Spring resolves the name attribute of the @Resource annotation to the
name of the bean, and the type attribute resolves to 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
, the byName automatic injection strategy will be used through the reflection mechanism.

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 put @Resource on the setter method, because this is more in line with the object-oriented thinking, and the
attributes are manipulated through set and get instead of directly.

@Resource assembly sequence:
①If name and type are specified at the same time, find the only matching bean from the Spring context for assembly, and throw
an exception if not found.
② If the name is specified, the bean with the matching name (id) will be searched from the context for assembly, and an exception will be thrown if it cannot be found.
③If type is specified, find the only bean that matches similarly from the context and assemble it. If no bean is found or more than one is found,
an exception will be thrown.
④ If neither name nor type is specified, it will be automatically assembled according to 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.
The role of @Resource is equivalent to @Autowired, except that @Autowired is automatically injected according to byType.

Guess you like

Origin blog.csdn.net/wjavadog/article/details/126456472
Recommended