spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

`FullyQualifiedConfigurationBeanNameGenerator` doesn't apply to `@Bean` methods discovered via `@ComponentScan`

Open kzander91 opened this issue 1 week ago • 0 comments

I'm trying to apply the new FullyQualifiedConfigurationBeanNameGenerator introduced in 7.0 (see #33448) through @ComponentScan(nameGenerator = FullyQualifiedConfigurationBeanNameGenerator.class), because I want its naming logic to not apply globally, but only for beans defined in certain packages. I have been unable to use it to achieve that goal, because @Bean methods declared in @Configuration classes that are discovered through a @ComponentScan seem to not use the generator.

For example:

package test.main;

@SpringBootApplication
public class Main {
  static void main(String[] args) {
    SpringApplication.run(Main.class, args);
  }

  @Configuration
  @ComponentScan(basePackages = "test.basepackage",
                 nameGenerator = FullyQualifiedConfigurationBeanNameGenerator.class)
  static class ComponentScanWithNameGenerator {}

}

However, the @Bean method in the following class (scanned by the @ComponentScan above) isn't named as expected:

package test.basepackage;

@Configuration
class SomeConfig {
  @Bean
  String someBean() { return "bean"; } // Will be named "someBean" instead of "test.basepackage.SomeConfig.someBean"
}

Now, Spring Boot allows me to globally configure a name generator like this:

  static void main(String[] args) {
    new SpringApplicationBuilder(Main.class)
      .nameGenerator(FullyQualifiedConfigurationBeanNameGenerator.INSTANCE)
      .run(args);
  }

This results in the @Bean method to be named as expected, however this now applies globally for everything and no longer just for the package(s) I want, which is incompatible with for example WebMVC, which internally depends on beans having particular names: https://github.com/spring-projects/spring-framework/blob/b038beb85490c8a80711b1a6c8cfffbb21276b3e/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java#L266-L286

Am I missing something here? Is this not an intended use case to scope this particular name generator to only part of my app? Otherwise, as things stand, using the new name generator seems to be impractical in typical applications...

kzander91 avatar Dec 02 '25 09:12 kzander91