views:

3192

answers:

2

I haven't gotten my head wrapped around Spring yet, so correct me if this question doesn't make sense...

I have a PropertyPlaceholderConfigurer

<bean id="rdbmPropertiesPlacholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false">
    <property name="location" value="classpath:/properties/rdbm.properties" />
</bean>

And I have a bean being injected I guess?

<bean id="PortalDb" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${hibernate.connection.driver_class}" />
    <property name="url" value="${hibernate.connection.url}" />
    <property name="username" value="${hibernate.connection.username}" />
    <property name="password" value="${hibernate.connection.password}" />
    ...

What I want is a second placeholder pointing to a different properties file with the username/password so that I can split up the properties into two different files. Then the database connection information can be separate from the db username/password, and I can source control one and not the other.

I've tried basically copying the rdbmPropertiesPlaceholder with a different id and file and trying to access the properties, but it doesn't work.

This code is from the uPortal open source web portal project.

+5  A: 

Using this notation lets you specify multiple files:

 <bean id="rdbmPropertiesPlacholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="false">
     <property name="locations">
       <list>
           <value>classpath:/properties/rdbm.properties</value>
           <value>classpath:/properties/passwords.properties</value>
       </list>
    </property>
 </bean>

The propertyplaceholderconfigurerer just merges all of these to look like there's only one, so your bean definitions do not know where the properties come from.

krosenvold
That syntax seems ok, but it doesn't seem to load my second file. Not really sure what's going on...
Sam Hoice
Ok, so I think I have it figured out. The same thing is done in two places, but it only seems to have an effect in the one I wasn't looking at. Thanks, I think this fixed it!
Sam Hoice
+3  A: 

The org.springframework.beans.factory.config.PropertyPlaceholderConfigurer can do this (as already answered. What you may want to do is make use of the name spacing so that you can refer to same-named properties from both files without ambiquity. For your example, you can do this:

<bean id="generalPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:/properties/general.properties"/>
</bean>

<bean id="db.PropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:/properties/rdbm.properties" />
    <property name="placeholderPrefix" value="$db{" />
    <property name="placeholderSuffix" value="}" />  
</bean>

In your context files, you can now refer to general properties with ${someproperty}, and refer to rdbm properties with $db{someproperty}.

This will make your context files much cleaner and clearer to the developer.

Rolf