下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeanNamesForType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 测试profile
* 1)使用命令参数加载profile
* -Dspring.profiles.active=test
* 2)使用AnnotationConfigApplicationContext无参构造器
*/
@Test
public void test01() {
//AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigofProfile.class);
// 1.使用无参构造器创建对象
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
// 2.设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("dev");
// 3.加载主配置类
applicationContext.register(MainConfigofProfile.class);
// 4.刷新容器
applicationContext.refresh();
String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String name : beanNamesForType) {
System.out.println(name);
}
Yellow yellow = applicationContext.getBean(Yellow.class);
System.out.println(yellow);
}
@Test
public void testNoDefaultFactory() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
SampleConfiguration1.class);
String[] beanNamesForType = context
.getBeanNamesForType(FeignErrorDecoderFactory.class);
assertThat(beanNamesForType).isEmpty();
context.close();
}
@Test
public void disableFeignTracing() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TracerConfig.class, FeignTracingAutoConfiguration.class);
TestPropertyValues.of("opentracing.spring.cloud.feign.enabled:false").applyTo(context);
context.refresh();
String[] feignContextBeans = context.getBeanNamesForType(TraceFeignContext.class);
assertThat(feignContextBeans.length, is(0));
}
@Test
public void disableJmsTracing() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TracerConfig.class, JmsAutoConfiguration.class);
TestPropertyValues.of("opentracing.spring.cloud.jms.enabled:false").applyTo(context);
context.refresh();
String[] tracingJmsTemplateBeans = context.getBeanNamesForType(TracingJmsTemplate.class);
assertThat(tracingJmsTemplateBeans.length, is(0));
}