【问题标题】:Maven 3 different profiles - Spring propertyPlaceHolderConfig cannot use value from pom fileMaven 3 不同的配置文件 - Spring propertyPlaceHolderConfig 不能使用 pom 文件中的值
【发布时间】:2011-09-13 09:48:07
【问题描述】:

我在使用 maven 3 和加载正确的 .properties 文件时遇到了一些问题。

我想要实现的是:使用 mvn -Plocal 我想加载 setting-local.properties,如果它与 prod 一起运行,我想加载 settings-prod.properties。

它通过使用 mvn -Denv=local 工作,但是当我尝试使用 -Plocal 时,变量没有加载(settings-${env}.properties 不存在)。

我的 pom.xml:

<profiles>
    <profile>
        <id>local</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <env>local</env>
        </properties>
    </profile>
</profiles>

在我的应用程序上下文中,我想加载环境变量:

<bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
                <list>
                        <value>classpath:settings-${env}.properties
                        </value>
                </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders">
                <value>true</value>
        </property>
</bean>

那么问题是什么,不应该双向工作吗?

【问题讨论】:

标签: spring maven-3


【解决方案1】:

我认为您将 Spring 的 PropertyPlaceholderConfigurerMaven's Filtering 机制混淆了。这些是相似但完全独立的机制(但它们可以一起使用)。

  • Spring 的 PropertyPlaceholderConfigurer 允许您从属性文件中获取值以在 Spring 应用程序上下文中使用。

  • Maven 的过滤允许您将文本文件(包括属性文件)中的值替换为 Maven 属性和环境中的值。

您可以将它们组合起来,但它会变成一个两阶段的过程。您的 Maven 构建使用过滤将值放入属性文件中,然后 Spring 可以读取这些值。 困惑? (我是)

【讨论】:

【解决方案2】:

Maven 项目属性不是系统属性,因此不能开箱即用。

您可以做的一件事是使用 Maven Resource Filtering 替换 Spring Context 文件中的属性键。或者,更好的是,拥有一个您可以从 Spring 中过滤和引用的属性文件。

【讨论】:

  • 我想要做的,也尝试过没有成功的是使用过滤替换 ${env} 变量,但似乎 applicationcontext.xml 不会被过滤/替换。您对此有什么解决方案吗?
猜你喜欢
  • 2016-06-27
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 2012-11-19
  • 2017-12-21
  • 2016-03-15
  • 2012-03-11
相关资源
最近更新 更多