java - Spring property placeholder not working -


i've read similar issues on stacoverflow.com, none of solulutions helped me. following configuration use (maven project structure): src/main/resources/properties/app.properties file

#possible values: dev test prod mode: dev 

in spring configuration:

<context:property-placeholder location="classpath:properties/app.properties"/> <import resource="classpath:/spring/db/${mode}-datasource-config.xml"/> 

based on value of ${mode} want import corresponding datasource configuration file.

when run embedded tomcat7 using mvn clean install tomcat7:run command i'm getting error:

10, 2013 5:52:29 pm org.apache.catalina.core.standardcontext loadonstartup severe: servlet /springwebflow threw load() exception java.lang.illegalargumentexception: not resolve placeholder 'mode' in string value "classpath:/spring/db/${mode}-datasource-config.xml" 

the target/classes/properties/app.properties file exists.

i'm using intellij idea , in editor can click on "${mode}" in <import resource="classpath:/spring/db/${mode}-datasource-config.xml"/> , see value in property file. editor change ${mode} onto grey colored dev showing can recognize property value. in editor see: <import resource="classpath:/spring/db/dev-datasource-config.xml"/>

any ideas why i'm getting error , how can resolved?

property placeholders in imports resolved against enviroment variables or system properties only.

since version 3.1 can use applicationcontextinitializer add propertysources enviroment solve problem.

see http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/

other option same using profiles: http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/

edit

for example:

add initializer web.xml

<context-param>     <param-name>contextinitializerclasses</param-name>     <param-value>foo.bar.appcontextinitializer</param-value> </context-param> 

and initializer:

public class appcontextinitializer implements applicationcontextinitializer<configurablewebapplicationcontext> {          @override         public void initialize(configurablewebapplicationcontext applicationcontext) {             properties props;             try {                 props = propertiesloaderutils.loadallproperties("/some/path");                 propertiespropertysource ps = new propertiespropertysource("profile", props);                 applicationcontext.getenvironment().getpropertysources().addfirst(ps);             } catch (ioexception e) {                 // handle error             }         }     }  

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -