ComponentScan annotation is repeatable now
Issue: SPR-13151
This commit is contained in:
parent
aecb8b6c6b
commit
388bd87ef0
|
|
@ -18,6 +18,7 @@ package org.springframework.context.annotation;
|
|||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Repeatable;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
|
@ -54,6 +55,7 @@ import org.springframework.core.type.filter.TypeFilter;
|
|||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
@Repeatable(ComponentScans.class)
|
||||
public @interface ComponentScan {
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.context.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Container annotation that aggregates several {@link ComponentScan} annotations.
|
||||
*
|
||||
* <p>Can be used natively, declaring several nested {@link ComponentScan} annotations.
|
||||
* Can also be used in conjunction with Java 8's support for repeatable annotations,
|
||||
* where {@link ComponentScan} can simply be declared several times on the same method,
|
||||
* implicitly generating this container annotation.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.3
|
||||
* @see ComponentScan
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
public @interface ComponentScans {
|
||||
|
||||
ComponentScan[] value();
|
||||
|
||||
}
|
||||
|
|
@ -260,8 +260,10 @@ class ConfigurationClassParser {
|
|||
}
|
||||
|
||||
// Process any @ComponentScan annotations
|
||||
AnnotationAttributes componentScan = AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ComponentScan.class);
|
||||
if (componentScan != null && !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
|
||||
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
|
||||
sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
|
||||
if (!componentScans.isEmpty() && !this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
|
||||
for (AnnotationAttributes componentScan : componentScans) {
|
||||
// The config class is annotated with @ComponentScan -> perform the scan immediately
|
||||
Set<BeanDefinitionHolder> scannedBeanDefinitions =
|
||||
this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
|
||||
|
|
@ -272,6 +274,7 @@ class ConfigurationClassParser {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process any @Import annotations
|
||||
processImports(configClass, sourceClass, getImports(sourceClass), true);
|
||||
|
|
|
|||
|
|
@ -147,7 +147,7 @@ public class ComponentScanAnnotationIntegrationTests {
|
|||
@Test
|
||||
public void withCustomBeanNameGenerator() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(ComponentScanWithBeanNameGenenerator.class);
|
||||
ctx.register(ComponentScanWithBeanNameGenerator.class);
|
||||
ctx.refresh();
|
||||
assertThat(ctx.containsBean("custom_fooServiceImpl"), is(true));
|
||||
assertThat(ctx.containsBean("fooServiceImpl"), is(false));
|
||||
|
|
@ -159,6 +159,14 @@ public class ComponentScanAnnotationIntegrationTests {
|
|||
// custom scope annotation makes the bean prototype scoped. subsequent calls
|
||||
// to getBean should return distinct instances.
|
||||
assertThat(ctx.getBean(CustomScopeAnnotationBean.class), not(sameInstance(ctx.getBean(CustomScopeAnnotationBean.class))));
|
||||
assertThat(ctx.containsBean("scannedComponent"), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multiComponentScan() {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MultiComponentScan.class);
|
||||
assertThat(ctx.getBean(CustomScopeAnnotationBean.class), not(sameInstance(ctx.getBean(CustomScopeAnnotationBean.class))));
|
||||
assertThat(ctx.containsBean("scannedComponent"), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -241,7 +249,8 @@ public class ComponentScanAnnotationIntegrationTests {
|
|||
@ComponentScan
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public static @interface ComposedConfiguration {
|
||||
public @interface ComposedConfiguration {
|
||||
|
||||
String[] basePackages() default {};
|
||||
}
|
||||
|
||||
|
|
@ -255,6 +264,7 @@ public class ComponentScanAnnotationIntegrationTests {
|
|||
@Configuration
|
||||
@ComponentScan(basePackageClasses = example.scannable._package.class)
|
||||
class ComponentScanAnnotatedConfig {
|
||||
|
||||
@Bean
|
||||
public TestBean testBean() {
|
||||
return new TestBean();
|
||||
|
|
@ -264,6 +274,7 @@ class ComponentScanAnnotatedConfig {
|
|||
@Configuration
|
||||
@ComponentScan("example.scannable")
|
||||
class ComponentScanAnnotatedConfig_WithValueAttribute {
|
||||
|
||||
@Bean
|
||||
public TestBean testBean() {
|
||||
return new TestBean();
|
||||
|
|
@ -272,13 +283,16 @@ class ComponentScanAnnotatedConfig_WithValueAttribute {
|
|||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
class ComponentScanWithNoPackagesConfig {}
|
||||
class ComponentScanWithNoPackagesConfig {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "example.scannable", nameGenerator = MyBeanNameGenerator.class)
|
||||
class ComponentScanWithBeanNameGenenerator {}
|
||||
class ComponentScanWithBeanNameGenerator {
|
||||
}
|
||||
|
||||
class MyBeanNameGenerator extends AnnotationBeanNameGenerator {
|
||||
|
||||
@Override
|
||||
public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
|
||||
return "custom_" + super.generateBeanName(definition, registry);
|
||||
|
|
@ -287,9 +301,17 @@ class MyBeanNameGenerator extends AnnotationBeanNameGenerator {
|
|||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "example.scannable_scoped", scopeResolver = MyScopeMetadataResolver.class)
|
||||
class ComponentScanWithScopeResolver {}
|
||||
class ComponentScanWithScopeResolver {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "example.scannable_scoped", scopeResolver = MyScopeMetadataResolver.class)
|
||||
@ComponentScan(basePackages = "example.scannable_implicitbasepackage")
|
||||
class MultiComponentScan {
|
||||
}
|
||||
|
||||
class MyScopeMetadataResolver extends AnnotationScopeMetadataResolver {
|
||||
|
||||
MyScopeMetadataResolver() {
|
||||
this.scopeAnnotationType = MyScope.class;
|
||||
}
|
||||
|
|
@ -304,6 +326,7 @@ class MyScopeMetadataResolver extends AnnotationScopeMetadataResolver {
|
|||
excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ComponentScanWithCustomTypeFilter.class),
|
||||
lazyInit = true)
|
||||
class ComponentScanWithCustomTypeFilter {
|
||||
|
||||
@Bean
|
||||
@SuppressWarnings({ "rawtypes", "serial", "unchecked" })
|
||||
public static CustomAutowireConfigurer customAutowireConfigurer() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue