spring Spring应用程序上下文外部属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8270290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring application context external properties?
提问by Dennis Ich
i have a Spring application and its working well so far. Now i want the properties file in an external config folder and not in the packed jar to change things without the need to repack. This is what i got:
我有一个 Spring 应用程序,到目前为止它运行良好。现在我希望属性文件位于外部配置文件夹中,而不是在打包的 jar 中,无需重新打包即可更改内容。这是我得到的:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- <property name="locations" value="classpath:/springcontext.properties"/> -->
<property name="locations" value ="config/springcontext.properties" />
The outcommented one is working and the other one i dont get to work :/ Can someone help?
outcommented 一个正在工作,另一个我没有上班:/有人可以帮忙吗?
Edit: Thx 4 comments so far.
编辑:到目前为止有 4 条评论。
Maybe my question wasnt clear enough :). I perform a Maven build and everything will be packaged and i want this folder to be NOT in the package nut next to the outcomming jar and in this folder i want the properties file. possible?
也许我的问题不够清楚:)。我执行了一个 Maven 构建,所有东西都将被打包,我希望这个文件夹不在输出 jar 旁边的包 nut 中,在这个文件夹中我想要属性文件。可能的?
采纳答案by fmucar
<context:property-placeholder location="classpath*:spring/*.properties" />
If you place it somewhere in the classpath in a directory named spring (change names/dirs accordingly), you can access with above
如果您将它放在名为 spring 的目录中的类路径中的某个位置(相应地更改名称/目录),您可以使用上述访问
<property name="locations" value ="config/springcontext.properties" />
this will be pointing to web-inf/classes/config/springcontext.properties
这将指向 web-inf/classes/config/springcontext.properties
回答by masted
You can try something like this:
你可以尝试这样的事情:
<context:property-placeholder
location="${ext.properties.dir:classpath:}/servlet.properties" />
And define ext.properties.dirproperty in your application server / jvm, otherwise the default properties location "classpath:/" (i.e., classes dir of .jar or .war) would be used:
并ext.properties.dir在您的应用程序服务器/jvm 中定义属性,否则将使用默认属性位置“classpath:/”(即 .jar 或 .war 的类目录):
-Dext.properties.dir=file:/usr/local/etc/
BTW, very useful blog post.
顺便说一句,非常有用的博客文章。
回答by Prasanna Talakanti
You can use file prefix to load the external application context file some thing like this
您可以使用文件前缀来加载外部应用程序上下文文件,就像这样
<context:property-placeholder location="file:///C:/Applications/external/external.properties"/>
回答by Sridhar
Thisblog can help you. The trick is to use SpEL (spring expression language) to read the system properties like user.home, to read user home directory using SpEL you could use #{ systemProperties['user.home']}expression inside your bean elements. For example to access your properties file stored in your home directory you could use the following in your PropertyPlaceholderConfigurer, it worked for me.
这个博客可以帮助你。诀窍是使用 SpEL(spring 表达式语言)读取系统属性,例如user.home,使用 SpEL 读取用户主目录,您可以#{ systemProperties['user.home']}在 bean 元素中使用表达式。例如,要访问存储在主目录中的属性文件,您可以在 PropertyPlaceholderConfigurer 中使用以下内容,它对我有用。
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:#{ systemProperties['user.home']}/ur_folder/settings.properties</value>
</property>
</bean>
回答by Arun B Chandrasekaran
This question is kind of old, but wanted to share something which worked for me. Hope it will be useful for people who are searching for some information accessing properties in an external location.
这个问题有点老了,但想分享一些对我有用的东西。希望它对正在搜索访问外部位置的属性的某些信息的人有用。
This is what has worked for me.
这对我有用。
Property file contents:
PROVIDER_URL=t3://localhost:8003,localhost:8004applicationContext.xmlfile contents: (Spring 3.2.3)Note:
${user.home}is a system property from OS.<context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/> <bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> <prop key="java.naming.provider.url">${PROVIDER_URL}</prop> </props> </property> </bean>
属性文件内容:
PROVIDER_URL=t3://localhost:8003,localhost:8004applicationContext.xml文件内容:(Spring 3.2.3)注意:
${user.home}是来自 OS 的系统属性。<context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/> <bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> <prop key="java.naming.provider.url">${PROVIDER_URL}</prop> </props> </property> </bean>
${PROVIDER_URL}got replaced with the value in the properties the file
${PROVIDER_URL}被替换为文件属性中的值
回答by pap
One way to do it is to add your external config folder to the classpath of the java process. That's how I've often done it in the past.
一种方法是将外部配置文件夹添加到 java 进程的类路径。这就是我过去经常这样做的方式。
回答by fjkjava
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" />
This works for me. Local development machine path is C:\apps\tomcat\ath\ath_conf and in server /apps/tomcat/ath/ath_conf
这对我有用。本地开发机器路径为 C:\apps\tomcat\ath\ath_conf 并在服务器 /apps/tomcat/ath/ath_conf
Both works for me
两者都对我有用

