问题在 Spring 4.2.5 中开始时我试图注入一个具有多个实现的接口,下面是测试类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = VenueConfig.class)
public class BandEventTest {
@Autowired
@Qualifier("rush")
Band rush;
@Autowired
Venue venue;
@Test
public void assertBandNotNull() {
Assert.assertNotNull("Who will perform if there's no band !!!", rush);
}
@Test
public void assertVenueNotNull() {
Assert.assertNotNull("Without venue, there would be no event !!!", venue);
}
@Test
public void triggerEvent() {
venue.hostPerformance();
}
}
我有两个bean实现的接口Band。拉什和范海伦:
@Component("rush")
public class Rush implements Band {
/*
* (non-Javadoc)
*
* @see com.book.springinaction.chap2.autowiring.Band#play()
*/
public void play() {
// TODO Auto-generated method stub
System.out.println("Rush playing 'Limelight'");
}
}
尽管使用了限定符注释(请忽略大写/小写的当前限定符名称和Rush bean名称,但我徒劳地尝试了所有组合)。在read this thread关于未在App中设置QualifierAnnotationAutowireCandidateResolver后,我的怀疑得到了证实。自Spring 4.0以来的上下文
我希望适合下面的内容(即使在Spring文档中也提到过):
<bean id="customAutowireConfigurer" class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
<property name="customQualifierTypes">
<set>
<value>org.springframework.beans.factory.annotation.Qualifier</value>
</set>
</property>
</bean>
在我的下面的配置类没有XML,即JUST ANNOTATIONS
@Configuration
@ComponentScan
public class VenueConfig {
/*Does using @Bean for CustomAutowireConfigurer make sense ?*/
}
答案 0 :(得分:0)
我没有使用Spring注释,但是你指向另一个线程的指针非常有用,我做了一些研究。基于the Spring docs,我相信你可以产生同样的效果:
@Configuration
@ComponentScan
public class VenueConfig {
@Bean
public CustomAutowireConfigurer qualifierAutowireConfigurer() {
CustomAutowireConfigurer customAutowireConfig = new CustomAutowireConfigurer();
customAutowireConfig.setCustomQualifierTypes( Collections.singleton( Qualifier.class ));
return customAutowireConfig;
}
}