本文介绍了如何在Spring中读取具有相同键的多个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到一个简单的问题。我有两个属性文件,我想读取来创建两个数据源。然而,那些属性文件具有完全相同的键!我可以使用以下方法读取这两个文件:

I am facing a simple problem here. I have two properties files I want to read to create two datasources. Yet those properties files have exactly the same keys! I am able to read both the files using:

<context:property-placeholder
    location="classpath:foo1.properties,classpath:foo2.properties"/>

但是我无法访问正确的值:

But then I am not able to access the right value:

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${driver}" /> <!-- Which one? -->
    <property name="url" value="${url}" />                <!-- Which one? -->
    ...
</bean>

如何阅读我的属性,以便我可以使用 $等变量{foo1.driver} 并知道哪一个被调用?

How can I read my properties so that I can use variables such as ${foo1.driver} and know which one is called?

感谢您的帮助!

推荐答案

尝试这样的事情(未经测试):

Try something like this(not tested):

<bean id="config1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="ignoreUnresolvablePlaceholders" value="true"/>
       <property name="placeholderPrefix" value="${foo1."/>
       <property name="locations">
        <list>
          <value>classpath:foo1.properties</value>
        </list>
      </property>
    </bean>

    <bean id="config2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="ignoreUnresolvablePlaceholders" value="false"/>
       <property name="placeholderPrefix" value="${foo2."/>
       <property name="locations">
        <list>
          <value>classpath:foo2.properties</value>
        </list>
      </property>
    </bean>

这篇关于如何在Spring中读取具有相同键的多个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:11