【问题标题】:Spring Configuration xml and properties out of warSpring Configuration xml 和属性脱离战争
【发布时间】:2015-04-27 19:02:47
【问题描述】:

我的目标是使 SpringApplicationContext.xml 和所有特定于环境的属性文件脱离 war 文件,从而使 war 文件与环境无关。

1.外部化属性文件: 我想我需要做这样的事情..

<property name="searchSystemEnvironment" value="true" />
<property name="locations">
    <list>
        <value>file:///${MY_ENV_VAR_PATH}/my.app.config.properties</value>
    </list>
</property>

这里也有讨论,how to read System environment variable in Spring applicationContext

2。 Externalize SpringApplicationContext.xml:我可能会使用外部化配置的 Spring boots 功能。

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Q1:作为 Spring boot 的新手,我不确定是否可以安全地删除/取消选择 Spring boot 附带的所有不必要的功能并将它们应用到我的项目中?我看到 Spring boot 适用于从头开始的项目,就我而言,我正在从事一个成熟的项目。

Q2:外部化配置是一种反模式吗?我的上述方法是否朝着正确的方向前进?

【问题讨论】:

    标签: xml spring spring-mvc spring-boot


    【解决方案1】:

    由于我没有使用过Spring boot,我只能回答你的第二个问题。 外部化配置绝对不是反模式。它增加了灵活性并简化了在客户站点中的(重新)部署。事实上,我说的是外部化属性,而不是上下文配置。这是我之前使用的tomcat中外部化属性的示例:

    <bean name="appProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">  
            <property name="locations">
                <list>
                    <value>classpath:config-default/app.properties</value>
                    <value>file:${CATALINA_BASE}/conf/app-properties</value>                    
                </list>
            </property>
            <property name="ignoreResourceNotFound" value="true" />
        </bean>
    

    第一个条目是内部的并设置默认属性。第二个条目是指一个可选的外部化文件,客户可以对其进行编辑以覆盖默认属性。您还可以交付第一个文件的子集,其中包含您认为客户可以安全修改的属性。然后在每次更新中保留目标环境中的设置。

    【讨论】:

    • &lt;context:property-placeholder location="file:${catalina.home}/conf/app.properties"/&gt; 为我工作
    猜你喜欢
    • 2012-05-19
    • 2011-12-18
    • 2012-03-02
    • 2022-11-10
    • 2014-06-04
    • 2023-03-02
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多