Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeanNamesForType()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeanNamesForType() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: code   File: IOCTest_Profile.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 测试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);
}
 
Example 2
@Test
public void testNoDefaultFactory() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
			SampleConfiguration1.class);
	String[] beanNamesForType = context
			.getBeanNamesForType(FeignErrorDecoderFactory.class);
	assertThat(beanNamesForType).isEmpty();
	context.close();
}
 
Example 3
@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));
}
 
Example 4
@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));
}