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

[Enhancement] @Bean can only inject one dependency,can I inject many dependencies in one method? [SPR-17441] #21973

Open
spring-projects-issues opened this issue Oct 29, 2018 · 2 comments
Labels
in: core status: waiting-for-triage

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 29, 2018

Paleozoic opened SPR-17441 and commented

When inject two beans,such as the following code:

@Bean("bean1")
public Bean buildBean1(){
    Bean bean = new Bean();
    return bean ;
}
@Bean("bean2")
public Bean buildBean2(){
    Bean bean = new Bean();
    return bean ;
}

can I just write the code? such as:

@Beans
public Map<String,Bean> buildBeans(){
    Map<String,Bean> beans = new HashMap<>();
    beans.put("bean1",new Bean());
    beans.put("bean2",new Bean());
   return beans ;
}

sometimes we can not confirm how many beans to inject,we have to write complex BeanDefinition and then registerBeanDefinition.
if we have @Beans,everything is easy~


Affects: 5.1.1

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 29, 2018

Zhang Jie commented

If you don't confirm how many beans to inject, using @Component and @ComponentScan will help, it's not necessary to define every bean using @Bean.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 29, 2018

Paleozoic commented

can @Component  and @ComponentScan  inject beans dynamically?

 

we also need to write java code to define beans and then scan components.

I mean that we can only use the yml configuration to inject beans dynamically.

for example,the above code shows that @Beans can inject beans dynamically ,we just need to handle the yml configuration in the buildBeans method.

such as we inject the datasources dynamically…

@spring-projects-issues spring-projects-issues added status: waiting-for-triage type: enhancement in: core and removed type: enhancement labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: waiting-for-triage
Projects
None yet
Development

No branches or pull requests

1 participant