Introduce ConfigurationBeanNameGenerator for @Bean-annotated methods

Includes FullyQualifiedConfigurationBeanNameGenerator implementation.

Closes gh-33448
This commit is contained in:
Juergen Hoeller
2025-07-13 16:08:05 +02:00
parent b48f65aed6
commit 1145054971
10 changed files with 237 additions and 43 deletions
@@ -67,6 +67,21 @@ class AnnotationConfigApplicationContextTests {
assertThat(beans).hasSize(1);
}
@Test
void scanAndRefreshWithFullyQualifiedBeanNames() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setBeanNameGenerator(FullyQualifiedConfigurationBeanNameGenerator.INSTANCE);
context.scan("org.springframework.context.annotation6");
context.refresh();
context.getBean(ConfigForScanning.class.getName());
context.getBean(ConfigForScanning.class.getName() + ".testBean"); // contributed by ConfigForScanning
context.getBean(ComponentForScanning.class.getName());
context.getBean(Jsr330NamedForScanning.class.getName());
Map<String, Object> beans = context.getBeansWithAnnotation(Configuration.class);
assertThat(beans).hasSize(1);
}
@Test
void registerAndRefresh() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@@ -74,7 +89,22 @@ class AnnotationConfigApplicationContextTests {
context.refresh();
context.getBean("testBean");
context.getBean("name");
assertThat(context.getBean("name")).isEqualTo("foo");
assertThat(context.getBean("prefixName")).isEqualTo("barfoo");
Map<String, Object> beans = context.getBeansWithAnnotation(Configuration.class);
assertThat(beans).hasSize(2);
}
@Test
void registerAndRefreshWithFullyQualifiedBeanNames() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setBeanNameGenerator(FullyQualifiedConfigurationBeanNameGenerator.INSTANCE);
context.register(Config.class, NameConfig.class);
context.refresh();
context.getBean(Config.class.getName() + ".testBean");
assertThat(context.getBean(NameConfig.class.getName() + ".name")).isEqualTo("foo");
assertThat(context.getBean(NameConfig.class.getName() + ".prefixName")).isEqualTo("barfoo");
Map<String, Object> beans = context.getBeansWithAnnotation(Configuration.class);
assertThat(beans).hasSize(2);
}
@@ -598,6 +628,8 @@ class AnnotationConfigApplicationContextTests {
static class NameConfig {
@Bean String name() { return "foo"; }
@Bean(autowireCandidate = false) String prefixName() { return "bar" + name(); }
}
@Configuration