在spring项目中添加profile

目录

在spring项目添加profile 是很常见的事情

首先

在resource目录下新建:
application-dev.properties
application-sit.properties
application-uat.properties
application-pro.properties

这里写图片描述

然后需要 在 spring配置文件中引入对应的beans,必须放在 xml的最下面,否则会出错

    <!-- 开发环境配置文件 -->
    <beans profile="sit">
        <context:property-placeholder location="classpath*:application-sit.properties" />
    </beans>

    <!-- 本地环境配置文件 -->
    <beans profile="dev">
        <context:property-placeholder location="classpath*:application-dev.properties" />
    </beans>

    <!-- 本地环境配置文件 -->
    <beans profile="uat">
        <context:property-placeholder location="classpath*:application-uat.properties" />
    </beans>

    <!-- 本地环境配置文件 -->
    <beans profile="pro">
        <context:property-placeholder location="classpath*:application-pro.properties" />
    </beans>

最后在 web.xml中配置一下
这里写图片描述


以上已经可以做到 修改web.xml中的 值修改 打包 环境 ,但还是不智能

整合maven profile

首先 将 web.xml中的文件更改成

 <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>${profiles.active}</param-value>
  </context-param>

然后在 pom文件中 添加

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${compiler.version}</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warName>${project.artifactId}</warName>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <include>**/web.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- 本地开发环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>sit</id>
            <properties>
                <profiles.active>sit</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 测试环境 -->
            <id>uat</id>
            <properties>
                <profiles.active>uat</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>
    </profiles>

于是你就可以在 idea中使用 maven 配置的 环境 进行打包操作
这里写图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值