Set autowire-candidate, and construcor byType solve more of the same bean conflict

By the previous example, you can see, the use of automatic assembly byType and constructor, if the match is not unique to the bean, will go wrong, so if you do not solve this conflict, byType and construcor has limitations.

autowire-candidate attribute

candidate: candidate

By providing autowire-candidate = "false", can candidate a bean departing byType and construcor automatic assembly ( note, not including byName. If addicts bean provided byName automatic assembly, as long as the names match, even if provided autoWire-candidate will still be assembled. ).

<bean id="dependent" class="SpringTest.autoWire.byType.Dependent" autowire="byType"/>
<!-- 这个bean通过自动装配注入 -->
<bean id="one" class="SpringTest.autoWire.byType.Collaborator">
    <property name="info" value="This is collaboratorBean"/>
</bean>

<!-- 这个bean设置了autowire-candidate="false" 
     不参与byType和constructor自动装配
 -->
<bean id="two" class="SpringTest.autoWire.byType.Collaborator" autowire-candidate="false">
    <property name="info" value="This is collaboratorBean"/>
</bean>

≠ fitting can not automatically be injected

Provided autowire-candidate = "false", but can not byType constructor and automatic assembly is implanted. Through explicit ref或<ref/>still be injected through byName automatic assembly, it may be injected.

Published 213 original articles · won praise 116 · views 80000 +

Guess you like

Origin blog.csdn.net/qq2071114140/article/details/104249531