Skip to content

Commit 7557967

Browse files
committed
Stop using explicitly aliased value attribute as @⁠Component name
Prior to this commit, if a custom stereotype annotation was meta-annotated with @⁠Component and declared a local String `value` attribute that was explicitly configured (via @⁠AliasFor) as an override for an attribute other than @⁠Component.value, the local `value` attribute was still used as a convention-based override for @⁠Component.value. Consequently, a local `value` attribute was used as a custom @⁠Component name, even when that is clearly not the intent. To address that, this commit revises the logic in AnnotationBeanNameGenerator so that a `value` attribute which is explicitly aliased to something other than @⁠Component.value is no longer used as an explicit @⁠Component name. See gh-34317 Closes gh-34346
1 parent 4ba14ca commit 7557967

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

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

+8-17
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,16 @@ public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry
147147
key -> getMetaAnnotationTypes(mergedAnnotation));
148148
if (isStereotypeWithNameValue(annotationType, metaAnnotationTypes, attributes)) {
149149
Object value = attributes.get(MergedAnnotation.VALUE);
150-
if (value instanceof String currentName && !currentName.isBlank()) {
150+
if (value instanceof String currentName && !currentName.isBlank() &&
151+
!hasExplicitlyAliasedValueAttribute(mergedAnnotation.getType())) {
151152
if (conventionBasedStereotypeCheckCache.add(annotationType) &&
152153
metaAnnotationTypes.contains(COMPONENT_ANNOTATION_CLASSNAME) && logger.isWarnEnabled()) {
153-
if (hasExplicitlyAliasedValueAttribute(mergedAnnotation.getType())) {
154-
logger.warn("""
155-
Although the 'value' attribute in @%s declares @AliasFor for an attribute \
156-
other than @Component's 'value' attribute, the value is still used as the \
157-
@Component name based on convention. As of Spring Framework 7.0, such a \
158-
'value' attribute will no longer be used as the @Component name."""
159-
.formatted(annotationType));
160-
}
161-
else {
162-
logger.warn("""
163-
Support for convention-based @Component names is deprecated and will \
164-
be removed in a future version of the framework. Please annotate the \
165-
'value' attribute in @%s with @AliasFor(annotation=Component.class) \
166-
to declare an explicit alias for @Component's 'value' attribute."""
167-
.formatted(annotationType));
168-
}
154+
logger.warn("""
155+
Support for convention-based @Component names is deprecated and will \
156+
be removed in a future version of the framework. Please annotate the \
157+
'value' attribute in @%s with @AliasFor(annotation=Component.class) \
158+
to declare an explicit alias for @Component's 'value' attribute."""
159+
.formatted(annotationType));
169160
}
170161
if (beanName != null && !currentName.equals(beanName)) {
171162
throw new IllegalStateException("Stereotype annotations suggest inconsistent " +

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,14 @@ void generateBeanNameFromSubStereotypeAnnotationWithStringArrayValueAndExplicitC
150150
assertGeneratedName(RestControllerAdviceClass.class, "myRestControllerAdvice");
151151
}
152152

153-
@Test // gh-34317
153+
@Test // gh-34317, gh-34346
154154
void generateBeanNameFromStereotypeAnnotationWithStringValueAsExplicitAliasForMetaAnnotationOtherThanComponent() {
155-
// As of Spring Framework 6.2, "enigma" is incorrectly used as the @Component name.
156-
// As of Spring Framework 7.0, the generated name will be "annotationBeanNameGeneratorTests.StereotypeWithoutExplicitName".
157-
assertGeneratedName(StereotypeWithoutExplicitName.class, "enigma");
155+
assertGeneratedName(StereotypeWithoutExplicitName.class, "annotationBeanNameGeneratorTests.StereotypeWithoutExplicitName");
158156
}
159157

160-
@Test // gh-34317
158+
@Test // gh-34317, gh-34346
161159
void generateBeanNameFromStereotypeAnnotationWithStringValueAndExplicitAliasForComponentNameWithBlankName() {
162-
// As of Spring Framework 6.2, "enigma" is incorrectly used as the @Component name.
163-
// As of Spring Framework 7.0, the generated name will be "annotationBeanNameGeneratorTests.StereotypeWithGeneratedName".
164-
assertGeneratedName(StereotypeWithGeneratedName.class, "enigma");
160+
assertGeneratedName(StereotypeWithGeneratedName.class, "annotationBeanNameGeneratorTests.StereotypeWithGeneratedName");
165161
}
166162

167163
@Test // gh-34317

0 commit comments

Comments
 (0)