简体   繁体   中英

How to externalize properties file for web application(war)?

I have developed a webapplication which is having jsp and java code. Right now I have placed all the key-value into a env/lifecycle specific properties file (like conf-dev.properties,conf-stg.properties,conf-prod.properties).

I want to externalize these properties file so that it can be placed outside of war(without effecting the war). right now war file is tightly coupled with properties file. if i have to modify any thing i have to build and make war and deploy.

I have very limited access on deployment server machine (only have access for one folder where i can put my configuration files) & deployment process is handled by CI(jenkin & automated script).

I explored on internet and came to know that we can achieve this using spring, would like to know what is the best way to achieve this?

If I understand your question, then you can use Class.getResourceAsStream(String) the linked Javadoc says (in part)

This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String) .

外部化环境特定属性的更好方法是使用“ user.home”或“ user.dir”。

As you are using Spring I suppose you already use PropertyPlaceholderConfigurer . If not you should ;)

The location of a property file can be anything that can be resolved as spring Resource . This includes classpath, servletcontext and also file references as URIs (file:///... For absolute paths)

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="${config.file.location}" />
</bean> 

Thanks @Martin F..

Resolved :This is the final one i used and its working fine in dev,stage Env.

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="false"/> <property name="order" value="1"/> <property name="locations"> <list> <value>classpath:conf-${cisco.life}.properties</value> <value>file:///${openshift.home}/data/conf-${cisco.life}.properties</value> <value>file:${openshift.home}/data/conf-${cisco.life}.properties</value> </list> </property> </bean> .

and i used script action hook in openshift to set the lifecycle on system level.

appname= echo $OPENSHIFT_APP_NAME case "$appname" in *dev)export JAVA_OPTS_EXT="${JAVA_OPTS_EXT} -Dcisco.life=dev"; echo "setting up env life dev for " $appname ;; *stage)export JAVA_OPTS_EXT="${JAVA_OPTS_EXT} -Dcisco.life=stg; echo "setting up env life as stg for " $appname case "$appname" in *dev)export JAVA_OPTS_EXT="${JAVA_OPTS_EXT} -Dcisco.life=dev"; echo "setting up env life dev for " $appname ;; *stage)export JAVA_OPTS_EXT="${JAVA_OPTS_EXT} -Dcisco.life=stg; echo "setting up env life as stg for " $appname case "$appname" in *dev)export JAVA_OPTS_EXT="${JAVA_OPTS_EXT} -Dcisco.life=dev"; echo "setting up env life dev for " $appname ;; *stage)export JAVA_OPTS_EXT="${JAVA_OPTS_EXT} -Dcisco.life=stg; echo "setting up env life as stg for " $appname .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM