---- @ Resource annotation of

Original link: https: //blog.csdn.net/weixin_38237873/article/details/83650429

 

@Resource @Autowired and annotation of dependency injection are used. Just @AutoWried automatic injection press by type, and automatically injected by default @Resource byName.

@Resource has two important attributes, namely name and type

spring will be resolved to the name attribute name of the bean, and the type attribute were resolved to the type of bean. So if you use the name attribute is used byName automatic injection strategy, if you use the type attribute is used byType automatic injection strategy. If not specified, by reflex mechanisms for automatic injection strategy using byName.

Find the bean dependency injection @Resource :( rules to be used in the field, for example)

1. The name attribute is neither specified nor specify the type attribute, automatically Find by byName way. If the bean matching is not found, the fallback is to find a primitive type, if found injected.

At this name is the variable name

Examples of errors:

@Resource
Private String bucketName;
@Resource
Private String the styleName;
in this case arranged bean name is the value specified by the attribute name, rather than the value of id

<the bean ID = "bucketName" class = "java.lang.String">
<constructor-Arg value = "$ {oos.bucketName}" />
</ the bean>
<-! Image style name ->
<the bean ID = "styleName" class = "java.lang.String">
<constructor-Arg value = "$ {oos.styleName}" />
</ bean>
Why re-understood here, because before I always thought that corresponds to the configuration id attribute value of the document, until the configuration of the above two types String bean, when actually being given as follows: No qualifying bean of type [java.lang.String ] is defined: expected single matching bean but found 2: bucketName , styleName this is because the spring will go to bean element in the value of the name attribute and variable names consistent bean, but because not specify the name attribute, then it can not be found in the original type String to find, the results look to find the two, so on the error.

2. simply specify @Resource annotation name, the name of the press to find the name there with equal name attribute bean bean elements inside.

Good example

@Resource (name = "bucket")
Private String bucketName;
@Resource (name = "style")
Private String the styleName;
<the bean name = "bucket" class = "java.lang.String">
<value = constructor-Arg " oos.bucketName {} $ "/>
</ the bean>
<-! image style name ->
<the bean name =" style "class =" java.lang.String ">
<constructor-Arg value =" $ {OOS } .styleName "/>
</ bean>
3. @Resource annotation specifies only the type attribute, from the bean type context find a unique match for assembly, not found or no more found, will throw an exception

4. The name attribute specifies both @Resource another specified type, from the Spring context to find a unique match of bean assembled, can not find an exception is thrown


Guess you like

Origin www.cnblogs.com/ncwoniu/p/11525962.html