使用 spring 在战争之外读取属性文件 [英] Read property file outside war using spring
问题描述
在此处输入代码
我在 etc 文件夹中放置了一个属性文件.每个子模块中的myapplication.properties"和其他几个属性文件..我尝试执行以下操作
enter code here
I have a property file placed in the etc folder. "myapplication.properties" and few other property files in each sub module.. i am try to do the following
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>${config}</value>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
我正在尝试 mvn -Dconfig=~/my.properties jetty:run
属性是从 application.properties 读取的,但不是用于 config..
The properties are read from application.properties but not for config..
在运行应用程序时,我得到的 ${jdbc.url} 不正确.. 这个 url 存在于 my.properties ..如何实现?
While running application i get the ${jdbc.url} not correct .. This url is present in my.properties .. How can this be achieved ?
谢谢
推荐答案
这就是我所拥有的,运行它
This is what I had, to run it
<bean id="placeholderConfigConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${config}" />
</bean>
并在 MAVEN_OPTS 中添加 -Dconfig=/var//my.properties
并执行 mvn jetty:run
And add -Dconfig=/var//my.properties
in the MAVEN_OPTS.. and did mvn jetty:run
我发现的另一种解决方案......而不是进行冗长的配置
Another one line solution I found.. instead of making verbose configuration just do
<context:property-placeholder location="file:${config}"/>
这篇关于使用 spring 在战争之外读取属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!