Skip to content

Custom BeanNameGenerators are not considered for repository bean name generation [DATACMNS-1497] #1931

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

Closed
spring-projects-issues opened this issue Mar 13, 2019 · 6 comments
Assignees
Labels
in: repository Repositories abstraction type: bug A general bug

Comments

@spring-projects-issues
Copy link

slinstaedt opened DATACMNS-1497 and commented

Spring-boot allows to configure a custom BeanNameGenerator. This custom BeanNameGenerator is not picked up by spring-data, but rather the default implementation is still gonna being used. This leads to unexpected bean names at best, or subtile errors at worst, especially with spring-boot 2.0.x versions, which allow to override bean definitions by default.

I am not completely sure, if that's a bug or a feature, at least it is somehow unexpected. When I configure a custom BeanNameGenerator for spring, I would expect it to be used not only in a subset of spring-projects.

 

@SpringBootApplication
public class Application {
    public static final AnnotationBeanNameGenerator FQCN_BEAN_NAME_GENERATOR = new AnnotationBeanNameGenerator() {

        @Override
        protected String buildDefaultBeanName(BeanDefinition definition) {
            String beanClassName = definition.getBeanClassName();
            Assert.state(beanClassName != null, "No bean class name set");
            return beanClassName;
        }
    };

    public static void main(final String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.setBeanNameGenerator(FQCN_BEAN_NAME_GENERATOR);
        application.run(args);
    }
}

Affects: 2.1.5 (Lovelace SR5)

Issue Links:

  • DATACMNS-1228 Allow custom bean name generator for repositories
    ("supersedes")
  • DATAJPA-1232 Allow custom bean name generator for repositories
    ("supersedes")
@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

Spring Data repository naming uses a slightly different variant of the BeanNameGenerator as we have advanced needs to derive bean names based on our configuration. However, we use the standard AnnotationBeanNameGenerator as fallback. It doesn't look like there was API for us to use to lookup the BeanNameGenerator set on the ApplicationContext yet.

I'll forward that request to the core Spring team. /cc Juergen Hoeller

@spring-projects-issues
Copy link
Author

slinstaedt commented

Thanks for forwarding.

Interestingly org.springframework.context.annotation.ConfigurationClassPostProcessor also uses a custom BeanNameGenerator for registering any @Configuration beans, also using FQCN instead of simple ones. I can just guess why, but there have been probably a lot of bean name clashes with config classes. 

Generating bean names by using simple class names was in the past probably a good way to strengthen backward compatibility with applications, which still use name-based injection. As (qualified) type-based injection is probably more used these days, this kind of compatibility might not be necessary any more. 

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

We need Spring Framework tweaked to expose the BeanNameGenerator used for configuration class scanning forwarded into ImportBeanDefinitionRegistrars. Jürgen's already on it and we'll be upgrading to the refined API for Moore

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

There's a draft implementation available via 2.2.0.DATACMNS-1497-SNAPSHOT of the spring-data-commons artifact. It will require a Spring Boot application to use e.g. @EnableJpaRepositories explicitly as Boot doesn't forward the BeanNameGenerator yet through it's intermediate ImportBeanDefinitionRegistrar.

The fix contains a rather brittle check for the default BeanNameGenerator handed down as we'd like to stick to the AnnotationBeanNameGenerator by default and the default one tweaking the bean name generation to use fully qualified class names which would render the repository beans being named differently than any other scanned bean definition. I'll reach out to Jürgen if we can make this less brittle by exposing the default one as constant

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

Jürgen was so kind to introduce a dedicate constant to allow us to compare the generator passed around against the default one. I've just merged this to master, feel free to give the snapshots a spin

@spring-projects-issues
Copy link
Author

slinstaedt commented

My contract has ended, so I can't proof this to be functional for the original project, but according to a dummy project, custom BeanNameGenerators are never forwarded from AbstractRepositoryConfigurationSourceSupport.

Versions used:

  • spring-boot-starter-parent:2.2.0.BUILD-SNAPSHOT
  • with overridden: spring-data-commons:2.2.0.DATACMNS-1509-SNAPSHOT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: repository Repositories abstraction type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants