Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collect spring properties from multiple files for use on a single bean

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.

like image 366
Jonathan Adelson Avatar asked Feb 10 '09 21:02

Jonathan Adelson


People also ask

What is the correct way to access multiple properties in spring?

To handle this. You can then place @EnableConfigurationProperties(classes = MyProperties. class) on your main application class or another @Configuration and then @Autowire your MyProperties class where its needed.

Can spring boot have multiple properties files?

Spring boot provides a lot of features out of the box. Having multiple application. properties files is one of them.

How read multiple values from properties in spring boot?

We use a singleton bean whose properties map to entries in 3 separate properties files. This is the main class to read properties from multiple properties files into an object in Spring Boot. We use @PropertySource and locations of the files in the classpath. The application.


2 Answers

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.

like image 81
krosenvold Avatar answered Sep 22 '22 13:09

krosenvold


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.

like image 21
Rolf Avatar answered Sep 22 '22 13:09

Rolf