Although gh-20615 introduced the use of @AliasFor for @Component(value) in the built-in
stereotype annotations (@Service, @Controller, @Repository, @Configuration, and
@RestController), prior to this commit the framework did not actually rely on @AliasFor
support when looking up a component name via stereotype annotations. Rather, the
framework had custom annotation parsing logic in
AnnotationBeanNameGenerator#determineBeanNameFromAnnotation() which effectively ignored
explicit annotation attribute overrides configured via @AliasFor.
This commit revises AnnotationBeanNameGenerator#determineBeanNameFromAnnotation() so that
it first looks up @Component stereotype names using @AliasFor semantics before falling
back to the "convention-based" component name lookup strategy.
Consequently, the name of the annotation attribute that is used to specify the bean name
is no longer required to be `value`, and custom stereotype annotations can now declare an
attribute with a different name (such as `name`) and annotate that attribute with
`@AliasFor(annotation = Component.class, attribute = "value")`.
Closes gh-31089
@ -665,9 +665,18 @@ analogous to how the container selects between multiple `@Autowired` constructor
@@ -665,9 +665,18 @@ analogous to how the container selects between multiple `@Autowired` constructor
When a component is autodetected as part of the scanning process, its bean name is
generated by the `BeanNameGenerator` strategy known to that scanner. By default, any
Spring stereotype annotation (`@Component`, `@Repository`, `@Service`, and
`@Controller`) that contains a name `value` thereby provides that name to the
corresponding bean definition.
Spring stereotype annotation (`@Component`, `@Repository`, `@Service`, `@Controller`,
`@Configuration`, and so forth) that contains a non-empty `value` attribute provides that
value as the name to the corresponding bean definition.
[NOTE]
====
As of Spring Framework 6.1, the name of the annotation attribute that is used to specify
the bean name is no longer required to be `value`. Custom stereotype annotations can
declare an attribute with a different name (such as `name`) and annotate that attribute
with `@AliasFor(annotation = Component.class, attribute = "value")`. See the source code
declaration of `Repository#value()` for a concrete example.
====
If such an annotation contains no name `value` or for any other detected component
(such as those discovered by custom filters), the default bean name generator returns
@ -100,6 +102,12 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
@@ -100,6 +102,12 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
@ -123,6 +131,36 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
@@ -123,6 +131,36 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
@ -134,8 +172,7 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
@@ -134,8 +172,7 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {