如何在spring应用程序上下文xml文件中使用pom.xml中的属性?

时间:2017-07-06 15:41:37

标签: java xml spring maven

我有一个弹簧上下文文件,类似于下面给出的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder
                location="classpath:application.properties, classpath:application-${env}.properties"/>

    <context:annotation-config/>
</beans>

配置配置文件的pom.xml片段如下 -

        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>

我想在我的spring应用程序上下文文件中使用基于所选maven配置文件决定的属性'env'。

我该怎么做?

编辑: - 它是一个Spring网络应用

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

1 个答案:

答案 0 :(得分:1)

不直接回答你的问题,但是,如果你可以使用Spring Boot,我认为使用Spring Profiles可以轻松解决你想要做的事情。

基本上,您可以避免在Maven和XML中的多个配置文件中手动编码,让Spring为您完成。 Here是一个基本的例子。