Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeansOfType()
The following examples show how to use
org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeansOfType() .
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 File: PropertySourceDemo.java From geekbang-lessons with Apache License 2.0 | 6 votes |
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(); }
Example 2
Source File: Main.java From waltz with Apache License 2.0 | 5 votes |
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(); }
Example 3
Source File: TenantScopeIntegrationTest.java From tutorials with MIT License | 5 votes |
@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(); } }
Example 4
Source File: TenantScopeIntegrationTest.java From tutorials with MIT License | 5 votes |
@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(); } }
Example 5
Source File: AutoServiceRegistrationAutoConfigurationTests.java From spring-cloud-commons with Apache License 2.0 | 4 votes |
private void assertNoBean(AnnotationConfigApplicationContext context) { Map<String, AutoServiceRegistration> beans = context .getBeansOfType(AutoServiceRegistration.class); then(beans).isEmpty(); }