Loading...
Spring Framework Reference Documentation 7.0.2의 Using Generics as Autowiring Qualifiers의 한국어 번역본입니다.
아래의 경우에 피드백에서 신고해주신다면 반영하겠습니다.
감사합니다 :)
@Qualifier 어노테이션에 더해, Java generic 타입을 qualification의 암시적 형태로 사용할 수 있습니다. 예를 들어, 다음과 같은 설정이 있다고 가정해 보겠습니다:
1@Configuration 2public class MyConfiguration { 3 4 @Bean 5 public StringStore stringStore() { 6 return new StringStore(); 7 } 8 9 @Bean 10 public IntegerStore integerStore() { 11 return new IntegerStore(); 12 } 13}
1@Configuration 2class MyConfiguration { 3 4 @Bean 5 fun stringStore() = StringStore() 6 7 @Bean 8 fun integerStore() = IntegerStore() 9}
앞에서의 빈들이 generic 인터페이스(즉, Store<String>과 Store<Integer>)를 구현한다고 가정하면, Store 인터페이스를 @Autowire할 수 있고, 다음 예제에서 보이는 것처럼 generic이 qualifier로 사용됩니다:
1@Autowired 2private Store<String> s1; // <String> qualifier, injects the stringStore bean 3 4@Autowired 5private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean
1@Autowired 2private lateinit var s1: Store<String> // <String> qualifier, injects the stringStore bean 3 4@Autowired 5private lateinit var s2: Store<Integer> // <Integer> qualifier, injects the integerStore bean
generic qualifier는 list, Map 인스턴스 및 array를 autowiring할 때에도 적용됩니다. 다음 예제는 generic List를 autowire합니다:
1// Inject all Store beans as long as they have an <Integer> generic 2// Store<String> beans will not appear in this list 3@Autowired 4private List<Store<Integer>> s;
1// Inject all Store beans as long as they have an <Integer> generic 2// Store<String> beans will not appear in this list 3@Autowired 4private lateinit var s: List<Store<Integer>>
Fine-tuning Annotation-based Autowiring with Qualifiers
Using CustomAutowireConfigurer