@ Autowired and @ resource usage and differences

Source: Internet
Author: User

I. @ autowired:

1. Spring 2.5 introduces @ autowired annotations, which can mark class member variables, methods, and constructors to complete automatic assembly. Use @ autowired to remove the set and get methods.

This annotation means that spring can automatically help you omit the setter/getter method of the object referenced in the bean, and it will automatically help you set/get.
<Bean id = "userdao" class = "..."/>
<Bean id = "userservice" class = "...">
<Property name = "userdao">
<Ref bean = "userdao"/>
</Property>
</Bean>
In this way, you need to create a userdao setter/getter method in userservice.
If @ autowired is used, you only need to declare it in the Implementation class of userservice.
@ Autowired
Private iuserdao userdao;

PS:

1. @ autowired is automatically assembled Based on the type.

2. @ autowired can also be manually specified for byname injection. Use the @ qualifier tag, for example:

@ Autowired () @ qualifier ("basedao ")
Private basedao;


2. @ resource is equivalent to @ autowired, but @ autowired is automatically injected by bytype, while @ resource is automatically injected by byname by default. @ Resource has two attributes, namely name and type. Spring resolves the name attribute of @ resource annotation to the bean name, And the type attribute to the bean type. Therefore, if the name attribute is used, the automatic injection policy of byname is used, and the automatic injection policy of bytype is used when the type attribute is used. If neither name nor type is specified, the byname automatic injection policy is used through reflection. @ Resource Assembly Sequence
1. if both name and type are specified, a unique matching bean is found in the spring context for assembly. If no matching bean is found, an exception is thrown. if the name is specified, the bean with the matching name (ID) is searched in the context for assembly. If no name is found, an exception is thrown.
3. if the type is specified, an exception is thrown if the unique bean of type matching is found in the context for assembly. If no or more beans are found, an exception is thrown. if neither name nor type is specified, the system automatically performs the Assembly in byname mode. If no match is found, the system returns to the original type for matching. If the match exists, the system automatically assembles the data;

Iii. Differences between @ autowired and @ resource 1. @ autowired and @ resource can be used to assemble beans. Both can be written in fields or in the setter method.
2. @ autowired is assembled by type by default (This annotation belongs to spring). By default, dependency objects must exist. If null values are allowed, you can set its required attribute to false.
3. @ Resource (This annotation belongs to J2EE), which is assembled by default according to the security name. The name can be specified through the name attribute,
If the name attribute is not specified, when the annotation is written on the field, the field name is used by default to search by name. If the annotation is written on the setter method, the attribute name is used by default for assembly. Only beans with matching names can be assembled by type. However, once the name attribute is specified, it is only assembled by name.
PS: @ resource Annotation on the field, so you don't need to write the setter method, and this annotation belongs to J2EE, reducing the coupling with spring.

@ Autowired and @ resource usage and differences

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.