The role of @Resource is equivalent to @autowired, but @Autowired automatically injected by Bytype , and @Resource automatically injected by ByName by default . @Resource There are two attributes that are important, the name and type,spring the Name property of the @resource annotation to the bean's name, and the type attribute to the Bean's. So if you use the Name property, you use the ByName Auto-injection policy, and the Type property uses the Bytype auto-injection policy. If neither name nor the type attribute is specified, the ByName auto-injection policy is used through the reflection mechanism.
@Resource Assembly Order
1. If name and type are specified at the same time, the uniquely matched bean is found from the spring context to assemble, and an exception is thrown if it is not found
2. If name is specified, a bean matching the name (ID) from the context is assembled, and an exception is thrown if it is not found
3. If the type is specified, an exception is thrown when the unique bean matching the match is found in the context, cannot be found, or multiple occurrences are found
4. If neither name is specified and type is not specified, the assembly is automatically byname; if there is no match, the fallback is a match for the original type, and if the match is automatically assembled;
@Autowired (auto-assemble) annotation is an assembly of dependent objects by type, by default it requires that the dependent object must exist, and if null values are allowed, you can set it required property to false. If we want to use assembly by name, we can use it in conjunction with @qualifier (precise assembly) annotations. As follows:
@Autowired @Qualifier ("Persondaobean")
Private Persondao Persondao;
@Resource annotations, like @autowired, can also be labeled on the setter method of a field or property, but it is assembled by name by default. The name can be specified by the Name property of the @resource, and if the name attribute is not specified, when the annotation is labeled on the field, that is, the default fetch field names are searched for dependent objects as bean names, and when annotations are labeled on the property setter method, That is, by default the property name is searched for dependent objects as bean names.
@Resource (name= "Persondaobean")
Private Persondao persondao;//used on fields
The difference between @resource and @Authwired in Spring annotations