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.

Common denominator: 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.

The different points are as follows:

First of all, say @autowired @Autowired for spring to provide annotations, need to import Package:org.springframework.beans.factory.annotation.Autowired; Inject only according to Bytype.

Private Userdao  Userdao;   used on fields
@Autowired Public void Setuserdao (Userdao userdao) {/// for the setter method of the property on this . userdao= Userdao;}
Because the Setuserdao () method function has a Userdao parameter, @Autowired will look for beans that match their type, and will create Userdao beans as Setuserdao (Userdao Setuserdao) entry parameters.

The @Autowired annotation is to assemble a dependent object 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 annotations. As follows:

@Autowired  @Qualifier ("userdao")private Persondao  Persondao;

Besides, @resource.
@Resource is automatically injected by ByName by default, provided by the Java EE.
Need to import Package:javax.annotation.Resource
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, use the ByName auto-injection policy, and use the
The type attribute is used with 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 field @Resource (name="Userdao")publicvoid Setuserdao (Userdao Userdao) { // On the setter method for the property     this. userdao= Userdao;}

Note: It is best to put @resource on the setter method.
@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 is injected automatically by Bytype.

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.