Friday, September 24, 2010

Don't forget to append file:// properties you specified at runtime

I am using spring and supply properties at runtime to make my application flexible that can be deployed with different settings. The typical usage is to combine property placeholder and context place holder:


classpath:myappAnalyticsWS.properties
${runtime.config}


You can supply properties in runtime.properties and the resource can be a local file such as c:/runtime/customization-runtime.properties. The gotcha is that you could be careful to specify the schema. For instance:


export CATALINA_OPTS = "CATALINA_OPTS -D -Druntime.config=$CATALINA_HOME/cutomization-runtime.properties"


would not work. You would get error such as:

010-09-24 20:38:38,197 ERROR [main] context.ContextLoader (ContextLoader.java:215) - Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [$CATALINA_HOME/cutomization-runtime.properties]
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:553)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostP



Fortunately it is very easy to fix it by simply specifying the schema:

export CATALINA_OPTS = "CATALINA_OPTS -D -Druntime.config=file://$CATALINA_HOME/cutomization-runtime.properties"

No comments: