Read property file outside war using spring

試著忘記壹切 提交于 2020-01-02 06:17:20

问题


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 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>

I am trying to do mvn -Dconfig=~/my.properties jetty:run

The properties are read from application.properties but not for config..

While running application i get the ${jdbc.url} not correct .. This url is present in my.properties .. How can this be achieved ?

Thanks


回答1:


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
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="file:${config}" />
</bean>

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}"/> 



回答2:


I think this feature becomes available in spring 3.1 via the new Environment abstraction. See the following spring blog for details:

http://blog.springsource.com/2011/02/15/spring-3-1-m1-unified-property-management/.

If spring 3.1 is not an option you can hard-code the filename and path in the spring xml configuration file to some well-known location and then developers can symlink against it.



来源:https://stackoverflow.com/questions/7910355/read-property-file-outside-war-using-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!