Both @Resource and @autowired are used to inject beans .
In fact @resource is not a spring annotation, his package is javax.annotation.Resource need to Import. however, Spring supports the annotation Injection.
Common
Both can be written on the field and setter METHODS. If both are written on a field, there is no need to write the setter method.
If the @requiedOr @autowired writes the set method, the program goes inside the set Method.
however, if it is written on a field, it will not enter the set Method.
different points
@Autowired
@Autowired annotations areBytypeAssemble dependent objects by type, by default it requires dependent objectsmust exist,
If a null value is allowed, you can set itRequired property is False。
If you have more than one type of bean candidate, you need toLimitedOne of the candidates throws Nouniquebeandefinitionexception
If we want to use assembly by name, we can combine @QualifierAnnotations are used Together.
@Qualifier The qualifier descriptor can bemore FineParticle size control how to choose a candidate
By default (no @qualifier label is specified on the Bean) @QualifiervalueProperty will match the Beanidentifiers。
@Autowired Private Userdao userdao; // used on fields @Autowired public void Setuserdao (userdao userdao) {// for The setter method of the property = userdao; } @Autowired @Qualifier ("userdao") private Userdao userdao;
@Resource
@Resource is automatically injected by byname by name by default , provided by the Java Ee.
The @Resource has two important attributes: name and type, and spring resolves the name property of the @resource annotation to the Bean's name , while the type attribute resolves to the bean .
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 (name="userdao") private Userdao userdao; // used on fields @Resource (name="userdao") public void Setuserdao (userdao userdao) {/// for the setter method of the property This.userdao= userdao; }
@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 a type is specified, an exception is thrown when a unique bean with a matching match is found in the context, not found or multiple;
(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;
The function of @Resource is equivalent to @autowired, but @Autowired automatically injected by bytype.
Original Link:
Http://my.oschina.net/u/216467/blog/205951?fromerr=dvrI1CxX
http://blog.csdn.net/fw0124/article/details/49992067
Spring Annotations @Resource and @autowired