简体   繁体   English

如何外部化Web应用程序的属性文件(WAR)?

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

I have developed a webapplication which is having jsp and java code. 我已经开发了一个具有jsp和Java代码的web应用程序。 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). 现在,我已将所有键值放入特定于env / lifecycle的属性文件(例如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). 我想外部化这些属性文件,以便可以将其放置在war之外(而不影响war)。 right now war file is tightly coupled with properties file. 现在,war文件与属性文件紧密相关。 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). 我在部署服务器计算机上的访问权限非常有限(只能访问一个文件夹,我可以在其中放置配置文件),部署过程由CI(詹金和自动脚本)处理。

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? 我在互联网上进行了探索,得知我们可以使用Spring来实现这一目标,想知道实现这一目标的最佳方法是什么?

If I understand your question, then you can use Class.getResourceAsStream(String) the linked Javadoc says (in part) 如果我理解您的问题,则可以使用链接的Javadoc说的(部分) Class.getResourceAsStream(String)

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) . 如果此对象是由引导类加载器加载的,则该方法将委托给ClassLoader.getSystemResourceAsStream(java.lang.String)

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

As you are using Spring I suppose you already use PropertyPlaceholderConfigurer . 当您使用Spring时,我想您已经使用了PropertyPlaceholderConfigurer If not you should ;) 如果没有,你应该;)

The location of a property file can be anything that can be resolved as spring Resource . 属性文件的位置可以是任何可以作为spring Resource解析的位置。 This includes classpath, servletcontext and also file references as URIs (file:///... For absolute paths) 这包括类路径,servletcontext以及作为URI的文件引用(file:/// ...表示绝对路径)

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html 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.. 谢谢@马丁F ..

Resolved :This is the final one i used and its working fine in dev,stage Env. 解决 :这是我使用的最后一个,它在开发,阶段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> . <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. 我在openshift中使用了脚本操作钩子来设置系统级别的生命周期。

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 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回显$ 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 . 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Web应用程序:如何外部化属性文件? - Web app with Spring: how externalize properties file? 作为WAR在Springcat上部署的Spring Boot,如何针对不同配置文件(Dev,Test,Staging,Prod)外部化application.properties - Spring boot deployed as WAR on tomcat, How to Externalize application.properties for different profiles(Dev,Test,Staging,Prod) 如何将Spring Boot application.properties外部化到tomcat / lib文件夹 - How to externalize Spring Boot application.properties to tomcat/lib folder 如何在 Spring Cloud Data Flow 任务中外部化应用程序属性 - How to externalize application properties in Spring Cloud Data Flow taks 如何从主应用程序外部化语言属性和模板? - How to externalize language properties and templates from main application? 如何在 Spring 的 Tomcat Web 服务器中外部化 application.properties? - How to externalize application.properties in Tomcat webserver for Spring? 如何在Spring应用程序的战争之外保留属性文件 - How To Keep Properties File outside the war in spring application java 如何从我的战争文件中外部化 application.properties - java how externalise application.properties from my war file 外部化Spring Libraries Web应用程序 - Externalize Spring Libraries Web Application 如何外部化Jacoco JavaAgent的属性 - How to externalize the properties of a jacoco javaagent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM