オートワイヤー修飾子としてジェネリクスを使用する
@Qualifier アノテーションに加えて、Java ジェネリクス型を暗黙的な修飾形式として使用できます。例: 次の構成があるとします。
Java
Kotlin
@Configuration
public class MyConfiguration {
@Bean
public StringStore stringStore() {
return new StringStore();
}
@Bean
public IntegerStore integerStore() {
return new IntegerStore();
}
}@Configuration
class MyConfiguration {
@Bean
fun stringStore() = StringStore()
@Bean
fun integerStore() = IntegerStore()
} 前述の Bean が汎用インターフェース(つまり、Store<String> および Store<Integer>)を実装していると仮定すると、次の例に示すように、@Autowire で Store インターフェースを使用でき、汎用は修飾子として使用されます。
Java
Kotlin
@Autowired
private Store<String> s1; // <String> qualifier, injects the stringStore bean
@Autowired
private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean@Autowired
private lateinit var s1: Store<String> // <String> qualifier, injects the stringStore bean
@Autowired
private lateinit var s2: Store<Integer> // <Integer> qualifier, injects the integerStore bean リスト、Map インスタンス、配列をオートワイヤーする場合にも、一般的な修飾子が適用されます。次の例は、一般的な List をオートワイヤーします。
Java
Kotlin
// 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;// Inject all Store beans as long as they have an <Integer> generic
// Store<String> beans will not appear in this list
@Autowired
private lateinit var s: List<Store<Integer>>