org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeansOfType ( )源码实例Demo

下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeansOfType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: geekbang-lessons   文件: PropertySourceDemo.java
public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

    // 扩展 Environment 中的 PropertySources
    // 添加 PropertySource 操作必须在 refresh 方法之前完成
    Map<String, Object> propertiesSource = new HashMap<>();
    propertiesSource.put("user.name", "xiaomage");
    org.springframework.core.env.PropertySource propertySource = new MapPropertySource("first-property-source", propertiesSource);
    context.getEnvironment().getPropertySources().addFirst(propertySource);

    // 注册当前类作为 Configuration Class
    context.register(PropertySourceDemo.class);
    // 启动 Spring 应用上下文
    context.refresh();
    // beanName 和 bean 映射
    Map<String, User> usersMap = context.getBeansOfType(User.class);
    for (Map.Entry<String, User> entry : usersMap.entrySet()) {
        System.out.printf("User Bean name : %s , content : %s \n", entry.getKey(), entry.getValue());
    }
    System.out.println(context.getEnvironment().getPropertySources());
    // 关闭 Spring 应用上下文
    context.close();
}
 
源代码2 项目: waltz   文件: Main.java
void start() {
    // configure logging
    LoggingUtilities.configureLogging();

    ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);

    Map<String, Endpoint> endpoints = ctx.getBeansOfType(Endpoint.class);
    endpoints.forEach((name, endpoint) -> {
        LOG.info("Registering Endpoint: {}", name);
        endpoint.register();
    });

    Map<String, DataExtractor> extractors = ctx.getBeansOfType(DataExtractor.class);
    extractors.forEach((name, extractor) -> {
        LOG.info("Registering Extractor: {}", name);
        extractor.register();
    });


    new StaticResourcesEndpoint().register();

    LOG.info("Completed endpoint registration");

    registerExceptionHandlers();
    enableGZIP();
    enableCORS();

}
 
源代码3 项目: tutorials   文件: TenantScopeIntegrationTest.java
@Test
public final void whenRegisterScopeAndBeans_thenContextContainsFooAndBar() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    try {
        ctx.register(TenantScopeConfig.class);
        ctx.register(TenantBeansConfig.class);
        ctx.refresh();

        TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
        foo.sayHello();
        TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
        bar.sayHello();
        Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);

        assertThat(foo, not(equalTo(bar)));
        assertThat(foos.size(), equalTo(2));
        assertTrue(foos.containsValue(foo));
        assertTrue(foos.containsValue(bar));

        BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
        BeanDefinition barDefinition = ctx.getBeanDefinition("bar");

        assertThat(fooDefinition.getScope(), equalTo("tenant"));
        assertThat(barDefinition.getScope(), equalTo("tenant"));
    } finally {
        ctx.close();
    }
}
 
源代码4 项目: tutorials   文件: TenantScopeIntegrationTest.java
@Test
public final void whenComponentScan_thenContextContainsFooAndBar() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    try {
        ctx.scan("com.baeldung.customscope");
        ctx.refresh();

        TenantBean foo = (TenantBean) ctx.getBean("foo", TenantBean.class);
        foo.sayHello();
        TenantBean bar = (TenantBean) ctx.getBean("bar", TenantBean.class);
        bar.sayHello();
        Map<String, TenantBean> foos = ctx.getBeansOfType(TenantBean.class);

        assertThat(foo, not(equalTo(bar)));
        assertThat(foos.size(), equalTo(2));
        assertTrue(foos.containsValue(foo));
        assertTrue(foos.containsValue(bar));

        BeanDefinition fooDefinition = ctx.getBeanDefinition("foo");
        BeanDefinition barDefinition = ctx.getBeanDefinition("bar");

        assertThat(fooDefinition.getScope(), equalTo("tenant"));
        assertThat(barDefinition.getScope(), equalTo("tenant"));
    } finally {
        ctx.close();
    }
}
 
private void assertNoBean(AnnotationConfigApplicationContext context) {
	Map<String, AutoServiceRegistration> beans = context
			.getBeansOfType(AutoServiceRegistration.class);
	then(beans).isEmpty();
}