Differences between @ Autowired annotation and @ Resource annotation in Spring,

Source: Internet
Author: User

Differences between @ Autowired annotation and @ Resource annotation in Spring,

Differences between @ Autowired annotation and @ Resource annotation in Spring

Spring not only supports custom@ AutowiredAnnotations also support several annotations defined by JSR-250 specifications, which are@ Resource,@ PostConstructAnd@ PreDestroy.
@ 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.
2. If name is specified, query the bean with the matching name (id) in the context for assembly. If no name is found, an exception is thrown.
3. If type is specified, an exception will be thrown if a unique bean of type matching is found in the context for assembly and no or multiple beans can be found.
4. If neither name nor type is specified, the system automatically performs the Assembly in byName mode. If no match exists, the system returns to an original type for matching. If the match exists, the system automatically assembles the data;

 

@ Autowired:

1. Both @ 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, for example, @ Autowired (required = false). If you want to use name assembly, you can use the @ Qualifier annotation as follows:
A.@ Autowired () @ Qualifier ("baseDao ")
B.Private BaseDao baseDao;
3. @ Resource (This annotation belongs to J2EE). The installation name is assembled by default. The name can be specified through the name attribute. If the name attribute is not specified, when the annotation is written on a field, by default, the field name is used to find the installation name. If the annotation is written on the setter method, the attribute name is used for assembly by default. Only beans with matching names can be assembled by type. However, once the name attribute is specified, it is only assembled by name.
A.@ Resource (name = "baseDao ")
B.Private BaseDao baseDao;

It is recommended to use the @ Resource Annotation on the field, so you do not need to write the setter method, and this annotation belongs to J2EE, reducing the coupling with spring. In this way, the code looks more elegant.

Using the new feature of Spring2.5-Autowired can achieve rapid automatic injection without adding bean declarations in the xml document, which greatly reduces the maintenance of the xml document.

Related Article

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.