The easiest way to understand the difference between @Autowriter @Resourse @Qualifier in Spring

Insert picture description here
1. Both @Autowired and @Resource can be used for assembling beans. Both can be written on fields or setter methods.
2. @Autowired is assembled by type by default (this annotation belongs to the industry spring). By default, the dependent object must exist. If you want to allow null values, you can set its required property to false, such as: @Autowired (required = false), if we want to use name assembly we can use it in conjunction with @Qualifier annotation, as follows:

1 @Autowired() @Qualifier("baseDao")     
2 private BaseDao baseDao;    

3. @Resource (this annotation belongs to J2EE), the default security name is assembled, the name can be specified by the name attribute,
if the name attribute is not specified, when the annotation is written on the field, the field name is taken by default to search by name If the annotation is written on the setter method, the property name is used for assembly by default. When no bean matching the name is found, it is assembled according to the type. However, it should be noted that if the name attribute is specified, it will only be assembled according to the name.

@Resource(name="baseDao")     
private BaseDao baseDao;    

I like to use @Resource annotation on the field, and this annotation belongs to J2EE, reducing the coupling with spring. The most important such code looks elegant.

Published 5 original articles · Likes0 · Visits5

Guess you like

Origin blog.csdn.net/wangziman/article/details/105470560