|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
36 | 36 | import org.junit.Test;
|
37 | 37 |
|
38 | 38 | import org.springframework.aop.support.AopUtils;
|
| 39 | +import org.springframework.beans.BeansException; |
| 40 | +import org.springframework.beans.factory.BeanClassLoaderAware; |
| 41 | +import org.springframework.beans.factory.BeanFactory; |
| 42 | +import org.springframework.beans.factory.BeanFactoryAware; |
39 | 43 | import org.springframework.beans.factory.annotation.CustomAutowireConfigurer;
|
40 | 44 | import org.springframework.beans.factory.config.BeanDefinition;
|
| 45 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
41 | 46 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
42 | 47 | import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
| 48 | +import org.springframework.context.EnvironmentAware; |
| 49 | +import org.springframework.context.ResourceLoaderAware; |
43 | 50 | import org.springframework.context.annotation.ComponentScan.Filter;
|
44 | 51 | import org.springframework.context.annotation.ComponentScanParserTests.KustomAnnotationAutowiredBean;
|
45 | 52 | import org.springframework.context.annotation.componentscan.simple.ClassWithNestedComponents;
|
46 | 53 | import org.springframework.context.annotation.componentscan.simple.SimpleComponent;
|
47 | 54 | import org.springframework.context.support.GenericApplicationContext;
|
| 55 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 56 | +import org.springframework.core.env.Environment; |
| 57 | +import org.springframework.core.io.ResourceLoader; |
| 58 | +import org.springframework.core.type.classreading.MetadataReader; |
| 59 | +import org.springframework.core.type.classreading.MetadataReaderFactory; |
| 60 | +import org.springframework.core.type.filter.TypeFilter; |
48 | 61 | import org.springframework.tests.context.SimpleMapScope;
|
49 | 62 | import org.springframework.util.SerializationTestUtils;
|
50 | 63 |
|
@@ -177,6 +190,12 @@ public void withCustomTypeFilter() {
|
177 | 190 | assertThat(testBean.getDependency(), notNullValue());
|
178 | 191 | }
|
179 | 192 |
|
| 193 | + @Test |
| 194 | + public void withAwareTypeFilter() { |
| 195 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithAwareTypeFilter.class); |
| 196 | + assertTrue(ctx.getEnvironment().acceptsProfiles("the-filter-ran")); |
| 197 | + } |
| 198 | + |
180 | 199 | @Test
|
181 | 200 | public void withScopedProxy() throws IOException, ClassNotFoundException {
|
182 | 201 | AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
@@ -258,6 +277,47 @@ public void withBasePackagesAndValueAlias() {
|
258 | 277 | public static class ComposedAnnotationConfig {
|
259 | 278 | }
|
260 | 279 |
|
| 280 | + public static class AwareTypeFilter implements TypeFilter, EnvironmentAware, |
| 281 | + ResourceLoaderAware, BeanClassLoaderAware, BeanFactoryAware { |
| 282 | + |
| 283 | + private BeanFactory beanFactory; |
| 284 | + private ClassLoader classLoader; |
| 285 | + private ResourceLoader resourceLoader; |
| 286 | + private Environment environment; |
| 287 | + |
| 288 | + @Override |
| 289 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 290 | + this.beanFactory = beanFactory; |
| 291 | + } |
| 292 | + |
| 293 | + @Override |
| 294 | + public void setBeanClassLoader(ClassLoader classLoader) { |
| 295 | + this.classLoader = classLoader; |
| 296 | + } |
| 297 | + |
| 298 | + @Override |
| 299 | + public void setResourceLoader(ResourceLoader resourceLoader) { |
| 300 | + this.resourceLoader = resourceLoader; |
| 301 | + } |
| 302 | + |
| 303 | + @Override |
| 304 | + public void setEnvironment(Environment environment) { |
| 305 | + this.environment = environment; |
| 306 | + } |
| 307 | + |
| 308 | + @Override |
| 309 | + public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) { |
| 310 | + ((ConfigurableEnvironment) this.environment).addActiveProfile("the-filter-ran"); |
| 311 | + assertNotNull(this.beanFactory); |
| 312 | + assertNotNull(this.classLoader); |
| 313 | + assertNotNull(this.resourceLoader); |
| 314 | + assertNotNull(this.environment); |
| 315 | + return false; |
| 316 | + } |
| 317 | + |
| 318 | + } |
| 319 | + |
| 320 | + |
261 | 321 | }
|
262 | 322 |
|
263 | 323 |
|
@@ -340,6 +400,14 @@ public ComponentScanParserTests.KustomAnnotationAutowiredBean testBean() {
|
340 | 400 | }
|
341 | 401 | }
|
342 | 402 |
|
| 403 | +@Configuration |
| 404 | +@ComponentScan( |
| 405 | + basePackages = "org.springframework.context.annotation", |
| 406 | + useDefaultFilters = false, |
| 407 | + includeFilters = @Filter(type = FilterType.CUSTOM, classes = ComponentScanAnnotationIntegrationTests.AwareTypeFilter.class), |
| 408 | + lazyInit = true) |
| 409 | +class ComponentScanWithAwareTypeFilter {} |
| 410 | + |
343 | 411 | @Configuration
|
344 | 412 | @ComponentScan(basePackages = "example.scannable",
|
345 | 413 | scopedProxy = ScopedProxyMode.INTERFACES,
|
@@ -384,3 +452,5 @@ class ComponentScanWithMultipleAnnotationIncludeFilters2 {}
|
384 | 452 | basePackages = "example.scannable",
|
385 | 453 | basePackageClasses = example.scannable._package.class)
|
386 | 454 | class ComponentScanWithBasePackagesAndValueAlias {}
|
| 455 | + |
| 456 | + |
0 commit comments