Skip to content

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

@kzander91

Description

@kzander91

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:

@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping(
@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
@Qualifier("mvcApiVersionStrategy") @Nullable ApiVersionStrategy apiVersionStrategy,
@Qualifier("mvcConversionService") FormattingConversionService conversionService,
@Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping();
mapping.setOrder(0);
mapping.setContentNegotiationManager(contentNegotiationManager);
mapping.setApiVersionStrategy(apiVersionStrategy);
initHandlerMapping(mapping, conversionService, resourceUrlProvider);
PathMatchConfigurer pathConfig = getPathMatchConfigurer();
if (pathConfig.getPathPrefixes() != null) {
mapping.setPathPrefixes(pathConfig.getPathPrefixes());
}
return mapping;
}

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions