Spring Annotations @Resource and @autowired

Source: Internet
Author: User

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.


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 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 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:



Spring Annotations @Resource and @autowired

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.