Using Generics as Autowiring Qualifiers
In addition to the @Qualifier
annotation, you can use Java generic types
as an implicit form of qualification. For example, suppose you have the following
configuration:
-
Java
@Configuration
public class MyConfiguration {
@Bean
public StringStore stringStore() {
return new StringStore();
}
@Bean
public IntegerStore integerStore() {
return new IntegerStore();
}
}
Assuming that the preceding beans implement a generic interface, (that is, Store<String>
and
Store<Integer>
), you can @Autowire
the Store
interface and the generic is
used as a qualifier, as the following example shows:
-
Java
@Autowired
private Store<String> s1; // <String> qualifier, injects the stringStore bean
@Autowired
private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean
Generic qualifiers also apply when autowiring lists, Map
instances and arrays. The
following example autowires a generic List
:
-
Java
// Inject all Store beans as long as they have an <Integer> generic
// Store<String> beans will not appear in this list
@Autowired
private List<Store<Integer>> s;