Both @Resource and @autowired are used as bean injections, in fact @resource is not a spring annotation, its package is javax.annotation.Resource and needs to be imported, but spring supports the annotation injection.
1. Common Ground
Both can be written on the field and setter methods. If both are written on a field, then you do not need to write the setter method.
2, different points
(1) @Autowired
@Autowired the annotations provided for spring, the package org.springframework.beans.factory.annotation.Autowired needs to be imported and only injected as Bytype.
public class Testserviceimpl { //below two kinds of @autowired as long as the use of one can be @Autowired private Userdao Userdao;//For fields @Autowired public void Setuserdao (Userdao Userdao) {//For the method of the property This.userdao = Userdao; }}
@Autowired annotation is an assembly of dependent objects by type (Bytype), by default it requires that the dependent object must exist, and if null values are allowed, you can set its Required property to false. If we want to assemble by name (byname), we can use it in conjunction with @qualifier annotations. As follows:
public class Testserviceimpl { @Autowired @Qualifier ("Userdao") private Userdao Userdao;}
(2) @Resource
The @Resource is automatically injected by ByName by default, provided by the Java EE and needs to import the package Javax.annotation.Resource. @Resource has two important properties: Name and type, and spring resolves the name property of the @resource annotation to the name of the bean, while the type attribute resolves to the bean. Therefore, if you use the Name property, the ByName auto-injection policy is used, and the type attribute is used when you use the Bytype auto-injection policy. If neither the name nor the type attribute is established, then the ByName auto-injection strategy is used through the reflection mechanism.
public class Testserviceimpl { //below two types of @resource @Resource (name= "Userdao") private Userdao as long as one is used Userdao; Used on field @Resource (name= "Userdao") public void Setuserdao (Userdao Userdao) {//Setter method for properties This.userdao = Userdao; }}
Note: It is best to put @resource on the setter method, because it is more in line with object-oriented thinking, through set, get to manipulate properties, rather than directly to manipulate properties.
@Resource Assembly Order:
① if both name and type are specified, the uniquely matched bean is found from the spring context to assemble, and an exception is thrown if it is not found.
② If name is specified, the matching bean from the context looking for the name (ID) is assembled, and an exception is thrown if it is not found.
③ if type is specified, an exception is thrown when a unique bean similar to a match is found in the context for assembly, not found or multiple found.
④ If neither name is specified and type is not specified, the assembly is automatically byname, and if there is no match, the fallback is a match for the original type and is automatically assembled if matched.
The function of @Resource is equivalent to @autowired, but @autowired is injected automatically according to Bytype.
Spring Annotations @resource and @autowired difference comparison