【问题标题】:spring replace bean name in config file with value from property filespring用属性文件中的值替换配置文件中的bean名称
【发布时间】:2014-04-01 15:40:14
【问题描述】:

我有一个 bean 配置文件如下

<bean id="myFactory" class="com.public.Factory">
    <property name="dataSourceAdaptor" ref="${value.from.property file}Adaptor" />
</bean>

我如何做到这一点。

我在配置文件的顶部添加了以下内容

<util:properties id="myProperties" location="classpath:app.properties"/>

然后尝试使用 ${} 引用该值,但我收到一条错误消息,指出 ${value.from.property 文件}适配器不是有效的 bean

我不能将整个名称 (xyzAdaptor) 放在属性文件中,因为属性文件中的值是一个机构,并且每个机构有多个适配器。

例如 xzyDisplayAdaptor、xyzProductAdaptor、xyzDatasourceAdaptor

xyz 客户端可以更改为 abc 客户端,我希望能够将属性文件中的值更改为 abc,并且所有与 abc 相关的 bean 都将被注入。

【问题讨论】:

  • 使用从属性读取并返回相应适配器的FactoryBean 怎么样?

标签: spring


【解决方案1】:

util:properties 标记用于创建 java.util.Properties 的实例。我认为您需要的是 PropertyPlaceholderConfigurer。 例如,

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" scope="singleton">
           <property name="searchSystemEnvironment" value="true" />
           <property name="ignoreResourceNotFound" value="true" />
           <property name="locations">
                <list>
                    <value>classpath:app.properties</value>
                </list>
            </property>
</bean>

【讨论】:

    【解决方案2】:

    用 Spel 试试这个:

        <util:properties id="myProperties" location="classpath:app.properties"/>
    
        <bean id="myFactory" class="com.public.Factory">
            <property name="dataSourceAdaptor" ref="#{'${value.from.property file}'+'Adaptor'}" />
        </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2020-07-20
      • 2010-10-05
      • 2012-08-25
      • 2018-01-05
      • 1970-01-01
      • 2015-03-22
      相关资源
      最近更新 更多