Browse Source

Polishing

pull/31121/head
Sam Brannen 1 year ago
parent
commit
71ba7bc5e0
  1. 14
      spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java
  2. 10
      spring-context/src/test/java/org/springframework/context/annotation/AnnotationBeanNameGeneratorTests.java

14
spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java

@ -95,17 +95,17 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator { @@ -95,17 +95,17 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
*/
@Nullable
protected String determineBeanNameFromAnnotation(AnnotatedBeanDefinition annotatedDef) {
AnnotationMetadata amd = annotatedDef.getMetadata();
Set<String> types = amd.getAnnotationTypes();
AnnotationMetadata metadata = annotatedDef.getMetadata();
Set<String> annotationTypes = metadata.getAnnotationTypes();
String beanName = null;
for (String type : types) {
AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(amd, type);
for (String annotationType : annotationTypes) {
AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, annotationType);
if (attributes != null) {
Set<String> metaTypes = this.metaAnnotationTypesCache.computeIfAbsent(type, key -> {
Set<String> result = amd.getMetaAnnotationTypes(key);
Set<String> metaAnnotationTypes = this.metaAnnotationTypesCache.computeIfAbsent(annotationType, key -> {
Set<String> result = metadata.getMetaAnnotationTypes(key);
return (result.isEmpty() ? Collections.emptySet() : result);
});
if (isStereotypeWithNameValue(type, metaTypes, attributes)) {
if (isStereotypeWithNameValue(annotationType, metaAnnotationTypes, attributes)) {
Object value = attributes.get("value");
if (value instanceof String currentName && !currentName.isBlank()) {
if (beanName != null && !currentName.equals(beanName)) {

10
spring-context/src/test/java/org/springframework/context/annotation/AnnotationBeanNameGeneratorTests.java

@ -65,6 +65,11 @@ class AnnotationBeanNameGeneratorTests { @@ -65,6 +65,11 @@ class AnnotationBeanNameGeneratorTests {
assertGeneratedName(ComponentWithName.class, "walden");
}
@Test
void generateBeanNameWithNamedComponentWhereTheNameIsBlank() {
assertGeneratedNameIsDefault(ComponentWithBlankName.class);
}
@Test
void generateBeanNameWithJakartaNamedComponent() {
assertGeneratedName(JakartaNamedComponent.class, "myJakartaNamedComponent");
@ -80,11 +85,6 @@ class AnnotationBeanNameGeneratorTests { @@ -80,11 +85,6 @@ class AnnotationBeanNameGeneratorTests {
assertGeneratedName(DefaultNamedComponent.class, "thoreau");
}
@Test
void generateBeanNameWithNamedComponentWhereTheNameIsBlank() {
assertGeneratedNameIsDefault(ComponentWithBlankName.class);
}
@Test
void generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() {
assertGeneratedNameIsDefault(AnonymousComponent.class);

Loading…
Cancel
Save