Skip to content

Commit

Permalink
Add ComponentScan#nameGenerator alias on @SpringBootApplication
Browse files Browse the repository at this point in the history
This commit allows to customize the default BeanNameGenerator for
scanned components using @SpringBootApplication.

Closes gh-19346
  • Loading branch information
snicoll committed Jan 6, 2020
1 parent a0a4cbc commit ee75557
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Expand Up @@ -23,8 +23,11 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
Expand Down Expand Up @@ -104,6 +107,22 @@
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};

/**
* The {@link BeanNameGenerator} class to be used for naming detected components
* within the Spring container.
* <p>
* The default value of the {@link BeanNameGenerator} interface itself indicates that
* the scanner used to process this {@code @SpringBootApplication} annotation should
* use its inherited bean name generator, e.g. the default
* {@link AnnotationBeanNameGenerator} or any custom instance supplied to the
* application context at bootstrap time.
* @return {@link BeanNameGenerator} to use
* @see SpringApplication#setBeanNameGenerator(BeanNameGenerator)
* @since 2.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "nameGenerator")
Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

/**
* Specify whether {@link Bean @Bean} methods should get proxied in order to enforce
* bean lifecycle behavior, e.g. to return shared singleton bean instances even in
Expand Down
Expand Up @@ -18,6 +18,9 @@

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
Expand All @@ -28,6 +31,7 @@
* Tests for {@link SpringBootApplication @SpringBootApplication}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
class SpringBootApplicationTests {

Expand All @@ -45,6 +49,20 @@ void proxyBeanMethodsCanBeDisabled() {
assertThat(attributes.get("proxyBeanMethods")).isEqualTo(false);
}

@Test
void nameGeneratorDefaultToBeanNameGenerator() {
AnnotationAttributes attributes = AnnotatedElementUtils
.getMergedAnnotationAttributes(DefaultSpringBootApplication.class, ComponentScan.class);
assertThat(attributes.get("nameGenerator")).isEqualTo(BeanNameGenerator.class);
}

@Test
void nameGeneratorCanBeSpecified() {
AnnotationAttributes attributes = AnnotatedElementUtils
.getMergedAnnotationAttributes(CustomNameGeneratorConfiguration.class, ComponentScan.class);
assertThat(attributes.get("nameGenerator")).isEqualTo(TestBeanNameGenerator.class);
}

@SpringBootApplication
static class DefaultSpringBootApplication {

Expand All @@ -55,4 +73,13 @@ static class NoBeanMethodProxyingSpringBootApplication {

}

@SpringBootApplication(nameGenerator = TestBeanNameGenerator.class)
static class CustomNameGeneratorConfiguration {

}

static class TestBeanNameGenerator extends DefaultBeanNameGenerator {

}

}

0 comments on commit ee75557

Please sign in to comment.