From 2c619232b3a734510f2901bc2e01dc6bedf25275 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 27 Aug 2015 14:12:01 +0200 Subject: [PATCH] Add ComponentScan aliases on SpringBootApplication Add aliases for `@ComponentScan`attributes on `@SpringBootApplication` so that it is possible to customize how the component scan should be applied on the project. Previously, one would have to revert to `@EnableAutoConfiguration` to achieve the same result. Closes gh-3368 --- .../autoconfigure/SpringBootApplication.java | 22 +++++++++++++++++++ .../src/main/asciidoc/using-spring-boot.adoc | 3 +++ 2 files changed, 25 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java index d283147b868..bc34228f597 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/SpringBootApplication.java @@ -26,6 +26,7 @@ import java.lang.annotation.Target; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.AliasFor; /** * Indicates a {@link Configuration configuration} class that declares one or more @@ -61,4 +62,25 @@ public @interface SpringBootApplication { */ String[] excludeName() default {}; + /** + * Base packages to scan for annotated components. + *

Use {@link #scanBasePackageClasses} for a type-safe alternative to + * String-based package names. + * @return base packages to scan + * @since 1.3.0 + */ + @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") + String[] scanBasePackages() default {}; + + /** + * Type-safe alternative to {@link #scanBasePackages} for specifying the packages + * to scan for annotated components. The package of each class specified will be scanned. + *

Consider creating a special no-op marker class or interface in each package + * that serves no purpose other than being referenced by this attribute. + * @return base packages to scan + * @since 1.3.0 + */ + @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") + Class[] scanBasePackageClasses() default {}; + } diff --git a/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc b/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc index 1b0a7126e3f..90a11cf8c17 100644 --- a/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc +++ b/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc @@ -696,6 +696,9 @@ The `@SpringBootApplication` annotation is equivalent to using `@Configuration`, } ---- +NOTE: `@SpringBootApplication` also provides aliases to customize the attributes of +`@EnableAutoConfiguration` and `@ComponentScan`. + [[using-boot-running-your-application]]