Fork me on GitHub

Spring-MVC-环境切换

前言

开发的系统往往存在多个环境的切换,生产环境,开发环境,测试环境等,公用一个配置文件导致生产部署的时候需要修改配置
非常的麻烦,但是spring 提供了对多个环境的切换。

实现

在spring-mvc.xml中配置多个环境配置文件的路径,profile 等于各个环境的简写,location为配置文件的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

<!-- 开发环境配置文件 by zhangfei -->
<beans profile="dev">
<context:property-placeholder
location="classpath:/setting/dev-setting.properties" />
</beans>

<!-- 测试环境配置文件 by zhangfei -->
<beans profile="test">
<context:property-placeholder
location="classpath:/setting/test-setting.properties" />
</beans>
<!-- 生产环境配置文件 by zhangfei -->
<beans profile="production">
<context:property-placeholder
location="classpath:/setting/product-setting.properties" />
</beans>

实现方式

JVM参数方式

 tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通过设置active选择不同配置文件
VM参数方式:

1
set JAVA_OPTS="-Dspring.profiles.active=test"

eclipse 中启动tomcat。项目右键 run as –> run configuration–>Arguments–> VM arguments中添加。local配置文件不必上传git追踪管理

1
-Dspring.profiles.active="local"

web.xml方式:

1
2
3
4
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>

-------------The End-------------

本文标题:Spring-MVC-环境切换

文章作者:Fancy君

发布时间:2018年09月03日 - 11:26:31

最后更新:2018年09月03日 - 11:38:18

原始链接:http://www.fancyiscrying.top/2018/09/03/Spring-MVC-环境切换/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。