Spring加载基于tomcat servlet上下文定义的application.properties

che*_*ist 5 java spring properties configuration-files

我需要为我们的春季项目进行开发和生产设置.我知道您可以使用弹簧配置文件,但这不是我们可以做的事情.

我想要做的是在开发环境中放置一个test-application.properties文件,并在生产中放置一个prod-application.properties文件.在tomcat上下文定义中,我们发送了以下内容:

<Context>
    <context-param>
        <param-name>properties_location</param-name>
        <param-value>file:C:\Users\Bill\test-application.properties</param-value>
    </context-param>
</Context>
Run Code Online (Sandbox Code Playgroud)

我们可以为生产服务器更改值.在spring配置中我们有这样的东西:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>${properties_location}</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="false" />
</bean>
Run Code Online (Sandbox Code Playgroud)

但我们不断收到如下错误:

org.springframework.beans.factory.BeanInitializationException:无法加载属性; 嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/ $ {properties_location}]

关于如何解决的任何想法?

And*_*sov 8

PropertyPlaceholder的一个功能是您可以定义多个资源位置.例如,您可以定义your-production-config.properties以及文件:C:/ Users/$ {user.name} /test-application.properties

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:your-production-config.properties</value>
            <value>file:C:/Users/${user.name}/test-application.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>        
</bean>
Run Code Online (Sandbox Code Playgroud)

对于生产,你需要将prod配置放在某个地方的classpath中(确切地说,确切地说,这并不重要,只是类路径) - 对于本地环境,你可以使用像这个文件的对应:C:/ Users/$ {user.name} /test-application.properties