Skip to content

BeanNameGenerator does not apply to @Bean-annotated methods  #33448

Open
@kamildoleglo

Description

Affects: 6.1.12 (and AFAIK all previous Spring versions)


What I'm trying to do:

I'm trying to override the default naming behavior for Spring Beans. I want all beans to be named with a fully-qualified name, otherwise the beans may crash upon startup - they may have the same names, since the project I'm working on is quite large and a number of people is working on it simultaneously. Moreover we obfuscate some parts of the code, which may lead to methods having the same name (but different fully-qualified names) at runtime.

I have used the FullyQualifiedAnnotationBeanNameGenerator in the @SpringBoot configuration:

@SpringBootApplication(
 (...)
  nameGenerator = FullyQualifiedAnnotationBeanNameGenerator::class
)

and it works fine for all @Component classes. The issue is that names of beans provided by @Bean-annotated methods, e.g.

package some.package
@Bean
fun foo(): Bar = Bar()

are still derived from the method name, so in this example the bean will be named foo instead of some.package.foo.

The FullyQualifiedAnnotationBeanNameGenerator is then also misleading as it only covers Components (and all annotations that derive from it, like @Repository or @Service); maybe ComponentNameGenerator would be better in that case.

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: feedback-providedFeedback has been providedstatus: waiting-for-triageAn issue we've not yet triaged or decided on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions