Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stereotype annotations like @Service should add @AliasFor for @Component [SPR-16066] #20615

Closed
spring-projects-issues opened this issue Oct 13, 2017 · 3 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Yanming Zhou opened SPR-16066 and commented

@Service is a specialization of @Component, like @GetMapping is a specialization of @RequestMapping, @Service should add @AliasFor like @GetMapping

@Component
public @interface Service
{
  @AliasFor(annotation=Component.class)
  String value() default "";
}

We can get metadata from annotation via unified code

Component component = AnnotatedElementUtils.getMergedAnnotation(clazz, @Component.class);
String beanName = component.value();

Referenced from: commits 43b5e21, 625737f

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Juergen Hoeller, any reason not to apply the same change to annotations such as @Configuration, @RestController, @ControllerAdvice, @RestControllerAdvice, etc.?

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Good point :-) I've added similar declarations to @Configuration and @RestController (with the alias pointing to @Controller in the latter case). The controller advices don't have a value attribute with bean name semantics, so don't seem applicable here.

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

I've added similar declarations to @Configuration and @RestController (with the alias pointing to @Controller in the latter case).

Cool. Thanks!

The controller advices don't have a value attribute with bean name semantics, so don't seem applicable here.

MMmmmmmm... indeed. I just listed off stereotypes without actually checking the source code. ;-)

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0.1 milestone Jan 11, 2019
@sbrannen sbrannen changed the title Annotations like @Service should add @AliasFor for @Component [SPR-16066] Stereotype annotations like @Service should add @AliasFor for @Component [SPR-16066] Aug 22, 2023
sbrannen added a commit that referenced this issue Aug 27, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants