简体   繁体   English

如何根据Spring中的配置创建多个相同类型的bean?

[英]How to create multiple beans of same type according to configuration in Spring?

I'm trying to create a specified number of beans of a same type in Spring. 我正在尝试在Spring中创建指定数量的相同类型的bean。

I've tried: 我试过了:

@Bean(name = "beanList")
public List<MyBean> beanList(
        @Value("${number:1}") int number
        ) {
    List<MyBean> beanList = new ArrayList<>(number);
    for (int i = 0; i < number; i++) {
        beanList.add(new MyBean());
    }
    return beanList;
}

But this is not what expected. 但这不是预期的。

In this way, the bean "beanList" is maintained by spring context, instead of it's elements, so I can't specify a name and init method or destroy method for each element in the list. 这样,bean“ beanList”是由spring上下文维护的,而不是它的元素,因此我无法列表中的每个元素指定名称和init方法或destroy方法

Any ideas? 有任何想法吗?

You could have a look at BeanFactoryPostProcessor , I tried with following code and it's working just fine, Bean s depends on MyBean could also be autowire d: 您可以看一下BeanFactoryPostProcessor ,我尝试使用以下代码,它的工作正常, Bean取决于MyBean也可以是autowire d:

@Configuration
public class AppConfig implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        for (int i = 0; i < 3; i++) {
            System.out.println("register my bean: " + i);
            beanFactory.registerSingleton("bean-" + i, new MyBean("MyBean-" + i));
        }
    }
}

Since you have complete control of creation process of MyBean instance, you can simply pass other beans in through constructor if it's necessary. 由于您可以完全控制MyBean实例的创建过程,因此可以根据需要简单地将其他bean传递给构造函数。 Hope this could be helpful to you :-) 希望这对您有所帮助:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不定义每个 Spring bean 的情况下创建多个相同类型的 Spring bean - How do I create multiple Spring beans of the same type without defining each one 在 Spring 中需要多个相同类型的 bean - Required Multiple beans of same type in Spring 多个相同类型的Spring Components \\ Bean - Multiple Spring Components\ Beans of the same type 如何动态创建多个相同类型的bean然后收集/自动装配它们 - how to dynamically create multiple beans of same type then gather/autowire them 具有两个相同类型NoUniqueBeanDefinitionException的bean的Spring Configuration xml文件 - Spring Configuration xml file with two beans of the same type NoUniqueBeanDefinitionException Spring Framework:是否可以在没有@Configuration的情况下创建具有相同@Component的两个bean? - Spring Framework: is possible to create two beans of the same @Component without @Configuration? Java中的Spring配置-创建和使用2个相同类的bean,而不使用Autowired - Spring configuration in Java - Create and use 2 beans of same class NOT using Autowired 如何在Spring XML配置中收集和注入给定类型的所有bean - How to collect and inject all beans of a given type in Spring XML configuration Spring Boot Application如何在没有@Configuration类的情况下创建bean - How does a Spring Boot Application create beans without @Configuration class Spring - 注入2个相同类型的bean - Spring — inject 2 beans of same type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM