Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

java - How to read multiple properties having the same keys in 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>

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

Thanks for helping!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...