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-issuemaster opened this issue Oct 29, 2018 · 2 comments

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster 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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster 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-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster 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…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.