除了 @Qualifier 注解之外,您还可以使用 Java 泛型类型作为一种隐式的限定形式。例如,假设你有以下配置:
@Configuration
public class MyConfiguration {
@Bean
public StringStore stringStore() {
return new StringStore();
}
@Bean
public IntegerStore integerStore() {
return new IntegerStore();
}
}
假设前面的 Bean 实现了一个泛型接口,(即 Store<String>和 Store<Integer>),你可以@Autowire Store 接口,泛型被用作限定词,如下例所示:
@Autowired
private Store<String> s1; // <String> qualifier, injects the stringStore bean
@Autowired
private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean
通用修饰符也适用于自动装配列表、映射实例和数组:
// 注入所有 Store Bean,只要它们有一个 <Integer> 泛型。
// Store<String> Bean 将不会出现在这个列表中
@Autowired
private List<Store<Integer>> s;
 
                         
                                

