0
votes

I have two projects -- project-web, and project-service, both of them use Spring core 3.1.3 and have configurations to load properties from corresponding property files:

project-web -- Spring Integration based project, in its spring config file:

<context:property-placeholder location="WEB-INF/spring-integration/spring-integration.properties" ignore-resource-not-found="true" />    
<import resource="classpath*:META-INF/spring/applicationContext.xml" />

where the import is to include the spring configuration file from project-service, and in the project-service project, I have following configured:

<context:property-placeholder location="classpath:META-INF/application.properties, classpath:META-INF/db.properties"  ignore-resource-not-found="true"/>
<import resource="classpath:META-INF/spring/applicationContext-data.xml"/>

where the import to include Spring configuration for the DAOs, inside the applicationContext-data.xml I have:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${db.${db.type}.driver}" />
       <property name="url" value="${db.${db.type}.url}"/>
       <property name="username" value="${db.${db.type}.username}" />
       <property name="password" value="${db.${db.type}.password}" />       
    </bean>

When I run the unit tests for project-service, everything is fine, all the variables are resolved correctly without any problem. But when I run the project-web (project-service will be included as a .jar file in the WEB-INF/lib folder of project-web), it throws error during start up saying can't resolve ${db.type}:

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [META-INF/spring/applicationContext-data.xml]: Could not resolve placeholder 'db.type' in string value "db.${db.type}.driver" at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE] ......................

Note: I can't declare everything in the project-web, because project-service will be also used by other projects. Anyone know why in project-service works when it runs alone but not when included by the project-web? It can't resolve the nested variable ${db.type}

1

1 Answers

2
votes

The problem is that your first PropertyPlaceHolderConfigurer is trying to resolve the placeholder that needs to be resolved by the second one.

You can either use a different prefix for each one (e.g. !{ instead of ${ for one of them), or set

ignore-unresolvable="true"

on the first one - then it will leave the resolution to the other one.