Skip to content

Allow customizing the bean name for repository beans [DATACMNS-168] #649

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 May 4, 2012 · 6 comments
Assignees
Labels
in: repository Repositories abstraction type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link

Andreas Hackl opened DATACMNS-168 and commented

Its not possible to specify two repositories with the same name in different packages, because the beans are registered with the same name (uncapitalized non-qualified class name). So only the repository which was generated at least is in the bean definition or an BeanDefinitionStoreException occurs when bean definition overriding is not allowed.

Provide the ability to specify a specifiy bean name for a repository or a custom BeanNameGenerator strategy for bean name resolution. This is already possible in the core spring framework for naming autodetected components.

@Repository("myRepositoryBeanName")
public interface MyRepository extends Respository<...> {
    // ...
}

or

<beans ...>
    <jpa:repositories base-package="org.example" name-generator="org.example.MyNameGenerator" />
</beans>

Issue Links:

  • DATACMNS-169 Handle naming collisions of interfaces when scanning and registering repository bean definitions

Referenced from: commits 874da36, b4a553e

Backported to: 1.5 Final, 1.4.1

1 votes, 5 watchers

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

You definitiely have a point here but I'd like to lower the severity as there's an easy workaround by slightly renaming one of the candidate repository interfaces

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

The bean name to be used is now determined by an AnnotationBeanNameGenerator which inspects the Spring stereotype annotations or falls back to the simple interface name. Registering custom BeanNameGenerator is not supported yet but the currently implemented solution should provide enough functionality to naturally customize bean names if necessary

@spring-projects-issues
Copy link
Author

Diego Díez Ricondo commented

Are there any plans to allow registering a custom BeanNameGenerator? I feel the workaround using @Named a bit hardcoded. I am looking for somethin more 'dynamic'.
In our case, we have N repositories with the same name but in different feaures/packages (package visible to grant encapsulation), but with the currenly behaviour you could end up having name collisions.

I was thinking one of these options could replace the hardcoded delegate AnnotationBeanNameGenerator:

@EnableJpaRepositories(nameGenerator = MyCustomBeanNameGenerator.class)
new SpringApplicationBuilder(Application.class)
                .beanNameGenerator(new MyCustomBeanNameGenerator())
                .run(args);

Does it make sense?

@spring-projects-issues
Copy link
Author

Sascha Woo commented

I've create a PR for it #262

@spring-projects-issues
Copy link
Author

Oliver Drotbohm commented

Sascha Woo – Care to create a new ticket as this one has been resolved for quite some time already

@spring-projects-issues
Copy link
Author

Sascha Woo commented

Okay will create a new one and update the PR

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

No branches or pull requests

2 participants