简体   繁体   English

Spring-来自属性文件的Bean引用列表?

[英]Spring - List of bean references from properties files?

I have this: 我有这个:

<bean class="...">
    <constructor-arg name="beans">
        <list>
            <ref bean="beanA" />
            <ref bean="beanB" />
        </list>
    </constructor-arg>
</bean>

I want to configure the list via a properties file, something like: 我想通过属性文件来配置列表,例如:

Properties file:
beans=beanA,beanB


XML file:
<bean class="...">
    <constructor-arg name="beans">
        <list refs="${beans}" />
    </constructor-arg>
</bean>

Is something like this possible with Spring? Spring可能会发生这种情况吗?

Edit: Just to give some context in case there are alternative solutions to the problem, this is for an application that has to write to multiple databases, and I want to configure which databases are enabled in the properties file, so that I don't have to maintain separate XML files for dev/production. 编辑:仅在某些情况下可以解决该问题,这是针对必须写入多个数据库的应用程序,而我想配置在属性文件中启用了哪些数据库,这样我就不会必须为开发/生产维护单独的XML文件。

My first though is to use a FactoryBean where you inject the property ${beans} and the application context. 我的第一个方法是使用FactoryBean,在其中注入属性$ {beans}和应用程序上下文。 Then in the factory bean, you loop on on every bean id/name and you call the context to retrieve the bean by name/id. 然后,在工厂bean中,对每个bean id / name进行循环,然后调用上下文以按name / id检索bean。 Then you constructs your bean with your constructor and the list you have just built. 然后,使用构造函数和刚刚构建的列表来构造bean。

use annotation ie 使用注释即

@Value("${beans}") 
private Class<? extends YourBean>String[] beanList;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM