Skip to content
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

Introduce BeanNameGenerator based on fully qualified class name #24114

Closed
torsten-github opened this issue Dec 2, 2019 · 6 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided type: enhancement A general enhancement
Milestone

Comments

@torsten-github
Copy link

Affects: Version 5

@ComponentScan will register Beans with a name generated from the class of the component.

Problem:
Having two classes with the same name UserMapper in different packages will result in an BeanDefinitionStoreException

  1. org.company.dom -> userMapper
  2. org.company.web -> userMapper

Suggestion:
Include the package in the bean name:

  1. org.company.dom.userMapper
  2. org.company.web.userMapper

Why:
I'm forced to duplicate package information in the class name:

  1. org.company.dom; class domUserMapper
  2. org.company.web; class webUserMapper

same giving each Component a package prefixed name:

  1. org.company.dom; @Component("domUserMapper") class userMapper
  2. org.company.web; @Component("webUserMapper") class userMapper

Especially with generated sources like MapStruct this is not possible.

Or should I write a custom BeanNamingStrategy. For this I do not find so much information, so I guess it's not that famous...

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 2, 2019
@sbrannen
Copy link
Member

sbrannen commented Dec 2, 2019

Implementing a custom BeanNameGenerator is a perfectly viable solution to this kind of problem. I've seen that done in real-world applications.

You can register a custom implementation via @ComponentScan(nameGenerator =...).

Does that meet your needs?

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) status: waiting-for-feedback We need additional information before we can continue labels Dec 2, 2019
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Dec 9, 2019
@torsten-github
Copy link
Author

torsten-github commented Dec 11, 2019

Yes, a custom BeanNameGenerator works perfectly. Thank you.

Maybe this would be a good enhancement for the spring-boot project. They could supply a property to choose a custom BeanNameGenerator... So I would save me the boilerplate code (10 Lines) in my main class.

For completness and to help others: This is my current solution:

@SpringBootApplication
@ComponentScan(nameGenerator = CustomGenerator.class)
public class App extends SpringBootServletInitializer {

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

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application
        .sources(App.class);
  }

  public static class CustomGenerator extends AnnotationBeanNameGenerator {
    @Override
    public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
      return definition.getBeanClassName();
    }
  }
}

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue labels Dec 11, 2019
@sbrannen
Copy link
Member

Yes, a custom BeanNameGenerator works perfectly. Thank you.

Great! And... you're welcome.

In light of that, I am closing this issue.

I also see that you raised spring-projects/spring-boot#19346 requesting special support in Spring Boot.

@sbrannen sbrannen added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 11, 2019
@sbrannen sbrannen self-assigned this Dec 11, 2019
@philwebb
Copy link
Member

@sbrannen Do you think it's worth surfacing a fully-qualified variant similar ConfigurationClassPostProcessor.IMPORT_BEAN_NAME_GENERATOR that could be generally used?

@sbrannen
Copy link
Member

@philwebb, yes, I think that could be generally useful since I've seen custom ones in the wild a few times.

And you're right: no real need to have ConfigurationClassPostProcessor.IMPORT_BEAN_NAME_GENERATOR defined as an anonymous inner class.

Reopening to see if @jhoeller agrees.

@sbrannen sbrannen reopened this Dec 13, 2019
@sbrannen sbrannen added status: waiting-for-triage An issue we've not yet triaged or decided on type: enhancement A general enhancement and removed status: declined A suggestion or change that we don't feel we should currently apply labels Dec 13, 2019
@sbrannen sbrannen added for: team-attention and removed for: team-attention status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 6, 2020
@sbrannen sbrannen added this to the 5.2.3 milestone Jan 7, 2020
@sbrannen sbrannen changed the title Enhancement for Bean name generation - include package name Introduce BeanNameGenerator based on fully qualified class name Jan 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: feedback-provided Feedback has been provided type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants