B、@Qualifierspring
org.springframework.beans.factory.annotation.Qualifierui
public @interface Qualifierspa
This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring. It may also be used to annotate other custom annotations that can then in turn be used as qualifiers..net
@Qualifier用於註釋一個字段或參數,當自動綁定時它做爲候選bean的限定器。它也能夠用於自定義的限定器註釋。orm
屬性get
valueit
B.一、舉例說明io
咱們使用@AutoWired按照類型自動綁定,當容器中有多個匹配的bean時,將綁定那一個呢?咱們能夠經過在須要綁定的字段或參數上使用@Qualifier(value=」 bar1」)來指定要綁定的bean。並在bean定義中使用<bean id="bar1"或<qualifier value="bar1"/>來關聯。class
範例1容器
1.Foo類
public class Foo {
@Autowired
@Qualifier(value="bar1")
private Bar bar;
}
2.配置文件
配置文件中有2個Bar,一個bean使用qualifier進行標註,一個使用id進行標註,容器將選擇和@Qualifier(value)匹配的bean來進行綁定。
<beans>
<context:annotation-config/>
<bean id="foo" class="x.y.Foo" />
<bean class="x.y.Bar">
<qualifier value="bar2"/>
<property name="a" value="bar2" />
</bean>
<bean id="bar1" class="x.y.Bar">
<property name="a" value="bar1" />
</bean>
</beans>
C、@Required
org.springframework.beans.factory.annotation.Required
public @interface Required
Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value.
標註一個方法(一般是一個JavaBean的setter方法)是@Required,也就是,這個setter方法必須定義爲經過一個值來依賴注入。