mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add ComponentScan#nameGenerator alias on @SpringBootApplication
This commit allows to customize the default BeanNameGenerator for scanned components using @SpringBootApplication. Closes gh-19346
This commit is contained in:
parent
a0a4cbce3a
commit
ee7555750e
@ -23,8 +23,11 @@ import java.lang.annotation.Retention;
|
||||
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;
|
||||
@ -104,6 +107,22 @@ public @interface SpringBootApplication {
|
||||
@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
|
||||
|
@ -18,6 +18,9 @@ package org.springframework.boot.autoconfigure;
|
||||
|
||||
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;
|
||||
@ -28,6 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link SpringBootApplication @SpringBootApplication}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class SpringBootApplicationTests {
|
||||
|
||||
@ -45,6 +49,20 @@ class SpringBootApplicationTests {
|
||||
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 {
|
||||
|
||||
@ -55,4 +73,13 @@ class SpringBootApplicationTests {
|
||||
|
||||
}
|
||||
|
||||
@SpringBootApplication(nameGenerator = TestBeanNameGenerator.class)
|
||||
static class CustomNameGeneratorConfiguration {
|
||||
|
||||
}
|
||||
|
||||
static class TestBeanNameGenerator extends DefaultBeanNameGenerator {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user