@Autowired difference and usage notes and annotations @Resource

Spring not only supports @Autowired annotation own definition, but also supports several annotations JSR-250 specification defined. Such as: @ Resource, @ PostConstruct and @PreDestroy

1. @Autowired
provided by a spring, but according to the injection byType

2. @Resource
by J2EE, is automatically injected in accordance with the default byName

@Resource has two important attributes, name and type:

Spring will @Resource annotation name attribute resolves to the bean's name, type attribute is resolved to the type of bean. Therefore, if the name attribute, using automated injection strategy byName; and a type attribute is used byType automatic injection strategy; If neither type attribute name is assigned, then use byName reflection by automatic injection strategy.

@Resource assembly sequence:

If you specify the name and type attributes, from the spring up and down and asked to find a unique match the bean assembly, if not found, an exception is thrown.
If the name attribute is specified, from the spring context find the name (id) match bean assembled, can not find an exception is thrown.
If you specify the type attribute, from spring to find a unique match the type of bean assembled context, you can not find or find more of an exception is thrown.
If neither name attribute, nor specify the type, the default is assembled according byName manner; if there is no match, it returns a primitive type is assembled, if a match is automatically assembled.
@Resource @Autowired effect is equivalent to, but is assembled in accordance with @Autowired byType.

3. difference
@Resource @Autowired and can be used to assemble bean, can be written is written in the field or on the setter method.
The default is @Autowired assembly according to the byType, it must rely on a default object exists, if allowed to empty, it may be required attribute set to false, if you want to use byName assembled, can be used in conjunction with @Qualifier annotation .
@Resource default is assembled in accordance with byName, name by name property can be developed, if you do not specify the name attribute, when notes written on the field, the default is to take the field name to find as the name. If the notes written on the setter method, the default is to take the name of the property be assembled, when only bean can not find a matching name for assembly by type. But it is worth noting that, once a name attribute is specified, it will be assembled by name.
Recommended @Resource annotation on the field, so you do not need to write a setter method, and this property is part of J2EE, which can reduce the coupling of the spring

4. Summary of the law
if @Resource @Autowired or write notes on top setter methods, the program will go inside the set method, if it is written in the filed, not to go inside the set paste method.
----------------
Disclaimer: This article is CSDN bloggers "do something" in the original article, follow the CC 4.0 BY-SA copyright agreements, please attach a reprint the original source link and this statement.
Original link: https: //blog.csdn.net/changqi008/article/details/83003669

Guess you like

Origin www.cnblogs.com/zhoading/p/11495677.html