Polishing

This commit is contained in:
Juergen Hoeller 2025-07-13 16:33:37 +02:00
parent 4063cb5b9a
commit 2434bb1540
5 changed files with 23 additions and 12 deletions

View File

@ -128,6 +128,7 @@ final class ConfigurationClass {
* @param metadata the metadata for the underlying class to represent * @param metadata the metadata for the underlying class to represent
* @param beanName name of the {@code @Configuration} class bean * @param beanName name of the {@code @Configuration} class bean
* @param scanned whether the underlying class has been registered through a scan * @param scanned whether the underlying class has been registered through a scan
* @since 6.2
*/ */
ConfigurationClass(AnnotationMetadata metadata, String beanName, boolean scanned) { ConfigurationClass(AnnotationMetadata metadata, String beanName, boolean scanned) {
Assert.notNull(beanName, "Bean name must not be null"); Assert.notNull(beanName, "Bean name must not be null");

View File

@ -18,10 +18,7 @@ package org.springframework.context.annotation;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -194,13 +191,19 @@ class ConfigurationClassBeanDefinitionReader {
AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class); AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class);
Assert.state(bean != null, "No @Bean annotation attributes"); Assert.state(bean != null, "No @Bean annotation attributes");
// Consider name and any aliases // Consider name and any aliases.
List<String> names = new ArrayList<>(Arrays.asList(bean.getStringArray("name"))); String[] explicitNames = bean.getStringArray("name");
String beanName = (!names.isEmpty() ? names.remove(0) : methodName); String beanName;
if (explicitNames.length > 0) {
// Register aliases even when overridden beanName = explicitNames[0];
for (String alias : names) { // Register aliases even when overridden below.
this.registry.registerAlias(beanName, alias); for (int i = 1; i < explicitNames.length; i++) {
this.registry.registerAlias(beanName, explicitNames[i]);
}
}
else {
// Default bean name derived from method name.
beanName = methodName;
} }
// Has this effectively been overridden before (for example, via XML)? // Has this effectively been overridden before (for example, via XML)?

View File

@ -64,8 +64,7 @@ class ConfigurationBeanNameTests {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.setBeanNameGenerator(new AnnotationBeanNameGenerator() { ctx.setBeanNameGenerator(new AnnotationBeanNameGenerator() {
@Override @Override
public String generateBeanName( public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
BeanDefinition definition, BeanDefinitionRegistry registry) {
return "custom-" + super.generateBeanName(definition, registry); return "custom-" + super.generateBeanName(definition, registry);
} }
}); });
@ -78,17 +77,22 @@ class ConfigurationBeanNameTests {
ctx.close(); ctx.close();
} }
@Configuration("outer") @Configuration("outer")
@Import(C.class) @Import(C.class)
static class A { static class A {
@Component("nested") @Component("nested")
static class B { static class B {
@Bean public String nestedBean() { return ""; } @Bean public String nestedBean() { return ""; }
} }
} }
@Configuration("imported") @Configuration("imported")
static class C { static class C {
@Bean public String s() { return "s"; } @Bean public String s() { return "s"; }
} }

View File

@ -20,4 +20,5 @@ import org.springframework.stereotype.Component;
@Component @Component
public class ComponentForScanning { public class ComponentForScanning {
} }

View File

@ -22,8 +22,10 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class ConfigForScanning { public class ConfigForScanning {
@Bean @Bean
public TestBean testBean() { public TestBean testBean() {
return new TestBean(); return new TestBean();
} }
} }