Property not found with multiple contexts: property-placeholder

I am using spring 3.1 with spring profiles to load beans. In my application context file, I load the following properties:

<context:property-placeholder order="1" location="classpath*:META-INF/spring/*_${spring.profiles.active}.properties" ignore-unresolvable="true"/> 

And then I use the property value to load the bean data source like

 <property name="driverClassName" value="${database.driverClassName}"/> 

It works great. The problem starts when I add a few extra property placeholders so that you can load properties from some database tables.

It uses a link to properties loaded

 <bean id="configFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean"> <constructor-arg ref="globalSystemConfiguration"/> </bean> 

To add to the details, this configFactoryBean uses a datasource to load properties from the database.

When I do this, I have the following exception:

 java.lang.ClassNotFoundException: ${database.driverClassName} 

My analysis is that it tries to load the datasource before resolving the property from the first placeholder of the context property. Maybe I'm wrong. Or perhaps the spring profile variable is not resolved properly.

Can anyone help me fix this.

Thanks Akki

+7
java spring spring-mvc hibernate
Jun 03 '13 at 8:31 on
source share
4 answers

This error for multiple property placeholders may be related to your problem: https://jira.spring.io/browse/SPR-9989

When using multiple PropertyPlaceholderConfigurer in combination with @Value annotation and default value for placeholder syntax (ie ${key:defaultValue} ), then only the first PropertyPlaceholderConfigurer used. If this configurator does not contain the required value, it crashes back to @Value by default, even if the second PropertyPlaceholderConfigurer contains the value.

Affect version / s: 3.1.3

+10
Mar 17 '14 at 11:21
source share
— -

In my application, I use configure-placeholder configurer as follows, and it works very well. You can try this.

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:META-INF/spring/*_${spring.profiles.active}.properties</value> </list> </property> </bean> 

I think this should solve your problem. :)

+4
Jun 03 '13 at 13:00
source share

Each <context: property-placeholder> creates a new instance of PropertyPlaceholderConfigurer - it becomes messy. You should have one such thing for each application and at the application level, and not for libraries, which simplifies maintenance.

For more information on how to deal with this, see here: http://rostislav-matl.blogspot.cz/2013/06/resolving-properties-with-spring.html

+4
Jun 03 '13 at 13:25
source share

Since you suggested hardcoding the path to the configuration file, try using the profiles attribute in the tag to selectively enable the configuration.

 <beans profile="profileName"> <context:property-placeholder order="1" location="classpath*:META-INF/spring/hardcoded.properties" ignore-unresolvable="true"/> </beans> <beans profile="profileName2"> <context:property-placeholder order="1" location="classpath*:META-INF/spring/hardcoded.properties" ignore-unresolvable="true"/> </beans> 

See the article explaining the profiles: http://java.dzone.com/articles/using-spring-profiles-xml

+1
Jun 03 '13 at 9:12
source share



All Articles