diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ComponentScan.java b/spring-context/src/main/java/org/springframework/context/annotation/ComponentScan.java index 0ef7d37aaa..a147cd1bf0 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ComponentScan.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ComponentScan.java @@ -157,10 +157,18 @@ public @interface ComponentScan { /** * The type of filter to use. *

Default is {@link FilterType#ANNOTATION}. + * @see #classes * @see #pattern */ FilterType type() default FilterType.ANNOTATION; + /** + * Alias for {@link #classes}. + * @see #classes + */ + @AliasFor(attribute = "classes") + Class[] value() default {}; + /** * The class or classes to use as the filter. *

The following table explains how the classes will be interpreted @@ -178,8 +186,12 @@ public @interface ComponentScan { * — for example, "include types annotated with {@code @Foo} OR {@code @Bar}". *

Specifying zero classes is permitted but will have no effect on component * scanning. + * @since 4.2 + * @see #value + * @see #type */ - Class[] value() default {}; + @AliasFor(attribute = "value") + Class[] classes() default {}; /** * The pattern (or patterns) to use for the filter, as an alternative diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanAnnotationParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanAnnotationParser.java index f96fd6219c..1119012b5b 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanAnnotationParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ComponentScanAnnotationParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * 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. @@ -47,6 +47,7 @@ import org.springframework.util.StringUtils; * * @author Chris Beams * @author Juergen Hoeller + * @author Sam Brannen * @since 3.1 * @see ClassPathBeanDefinitionScanner#scan(String...) * @see ComponentScanBeanDefinitionParser @@ -73,13 +74,12 @@ class ComponentScanAnnotationParser { public Set parse(AnnotationAttributes componentScan, final String declaringClass) { + Assert.state(this.environment != null, "Environment must not be null"); + Assert.state(this.resourceLoader != null, "ResourceLoader must not be null"); + ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry, componentScan.getBoolean("useDefaultFilters")); - - Assert.notNull(this.environment, "Environment must not be null"); scanner.setEnvironment(this.environment); - - Assert.notNull(this.resourceLoader, "ResourceLoader must not be null"); scanner.setResourceLoader(this.resourceLoader); Class generatorClass = componentScan.getClass("nameGenerator"); @@ -141,7 +141,7 @@ class ComponentScanAnnotationParser { List typeFilters = new ArrayList(); FilterType filterType = filterAttributes.getEnum("type"); - for (Class filterClass : filterAttributes.getClassArray("value")) { + for (Class filterClass : filterAttributes.getAliasedClassArray("classes", ComponentScan.Filter.class, null)) { switch (filterType) { case ANNOTATION: Assert.isAssignable(Annotation.class, filterClass, diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java index c4314401b3..b6be49d751 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java @@ -296,11 +296,12 @@ class MyScopeMetadataResolver extends AnnotationScopeMetadataResolver { } @Configuration -@ComponentScan(basePackages="org.springframework.context.annotation", +@ComponentScan( + basePackages="org.springframework.context.annotation", useDefaultFilters=false, - includeFilters=@Filter(type=FilterType.CUSTOM, value=ComponentScanParserTests.CustomTypeFilter.class), + includeFilters = @Filter(type = FilterType.CUSTOM, classes = ComponentScanParserTests.CustomTypeFilter.class), // exclude this class from scanning since it's in the scanned package - excludeFilters=@Filter(type=FilterType.ASSIGNABLE_TYPE, value=ComponentScanWithCustomTypeFilter.class), + excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ComponentScanWithCustomTypeFilter.class), lazyInit = true) class ComponentScanWithCustomTypeFilter { @Bean @@ -320,7 +321,7 @@ class ComponentScanWithCustomTypeFilter { @ComponentScan(basePackages="example.scannable", scopedProxy=ScopedProxyMode.INTERFACES, useDefaultFilters=false, - includeFilters=@Filter(type=FilterType.ASSIGNABLE_TYPE, value=ScopedProxyTestBean.class)) + includeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ScopedProxyTestBean.class)) class ComponentScanWithScopedProxy {} @Configuration