之前同事在项目里面正式环境测试环境配置文件都放在一起,每次发布都得注释一部分,让另一部分启用,随着配置文件内容的越来越多,很容易在发布时候出错,我摸索了半天实验出来一个方案,可供大家参考。本方案基于maven的spring.profiles.active功能,用的最简单粗暴方式使用,没有使用运行命令行动态更改的方案,说下具体的实现:
准备一个完整的SpringMVC项目
1.首先在web.xml里面配置正式环境 测试环境 开发环境:
1<!-- 环境切换专用: dev 开发环境 test:测试环境 prod:正式环境 -->
2<context-param>
3 <param-name>spring.profiles.active</param-name>
4<param-value>dev</param-value>
5</context-param>
2.applicationContext配置
在web.xml里面,我的配置是这样的
1<!-- spring -->
2 <context-param>
3 <param-name>contextConfigLocation</param-name>
4 <param-value>classpath:applicationContext.xml</param-value>
5 </context-param>
所以,到applicationContext.xml 里面,配置环境切换,
1<?xml version="1.0" encoding="UTF-8"?>
2<beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:p="http://www.springframework.org/schema/p"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xmlns:aop="http://www.springframework.org/schema/aop"
8 xmlns:context="http://www.springframework.org/schema/context"
9 xmlns:util="http://www.springframework.org/schema/util"
10 xmlns:cache="http://www.springframework.org/schema/cache"
11 xsi:schemaLocation="http://www.springframework.org/schema/beans
12 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
13 http://www.springframework.org/schema/context
14 http://www.springframework.org/schema/context/spring-context-4.0.xsd
15 http://www.springframework.org/schema/tx
16 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
17 http://www.springframework.org/schema/aop
18 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
19 http://www.springframework.org/schema/cache
20 http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
21 http://www.springframework.org/schema/util
22 http://www.springframework.org/schema/util/spring-util.xsd">
23
24 <beans profile="dev">
25 <import resource="dev/context-dev.xml" />
26 </beans>
27 <beans profile="prod">
28 <import resource="prod/context-prod.xml" />
29 </beans>
30</beans>
dev则运行开发环境配置,prod运行正式环境配置。
接下来拿context-dev.xml作为样例
3.context-dev.xml文件
1<?xml version="1.0" encoding="UTF-8"?>
2<beans
3 xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:p="http://www.springframework.org/schema/p"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xmlns:aop="http://www.springframework.org/schema/aop"
8 xmlns:context="http://www.springframework.org/schema/context"
9 xmlns:util="http://www.springframework.org/schema/util"
10 xmlns:cache="http://www.springframework.org/schema/cache"
11 xsi:schemaLocation="http://www.springframework.org/schema/beans
12 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
13 http://www.springframework.org/schema/context
14 http://www.springframework.org/schema/context/spring-context-4.0.xsd
15 http://www.springframework.org/schema/tx
16 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
17 http://www.springframework.org/schema/aop
18 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
19 http://www.springframework.org/schema/cache
20 http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
21 http://www.springframework.org/schema/util
22 http://www.springframework.org/schema/util/spring-util.xsd">
23
24 <context:property-placeholder location="classpath:dev/application-dev.properties" />
25
26 <context:annotation-config />
27 <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired,@Resource的属性被注入 -->
28 <context:component-scan base-package="com.naton" />
29
30 <import resource="classpath:spring-mongodb.xml"/>
31 <import resource="classpath:spring-shiro.xml"/>
32
33 <bean id="multipartResolver"
34 class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
35 <bean
36 class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
37</beans>
配置文件中清晰的写好了数据库的配置、权限的配置,当然,还有配置文件application-dev.properties,
application-dev.properties这个文件里面按照自己的需求来设计。
4.这时候你会发现Spring容器初始化确实完成了配置文件的切换,但是配置文件在项目里面也有使用,如何完成动态的切换?
我的方案:创建一个BaseContextListener 文件继承 ServletContextListener,配置到web.xml里面,然后在这个
BaseContextListener 文件里面获取web.xml里面配置的spring.profiles.active里面的参数
1// 这个方法在Web应用服务做好接受请求的时候被调用。
2 @Override
3 public void contextInitialized(ServletContextEvent event){
4 this.context = event.getServletContext();
5 System.out.println("The Simple Web App. Is Ready");
6 String env = this.context.getInitParameter("spring.profiles.active");
7 if (StringUtils.isBlank(env)) {
8 SystemConfig.ENV = "dev";
9 }else{
10 SystemConfig.ENV = env;
11 }
12 System.out.println("当前环境:" + SystemConfig.ENV);
13 }
这样容器启动时候就可以进行设置 SystemConfig.ENV的值了,接下来 就是SystemConfig.java文件的配置
5.SystemConfig.java文件:
1public static String ENV = "dev"; // 环境切换用
2static {
3 try {
4 PropertiesConfiguration config = null;
5 switch (ENV) {
6 case "dev":
7 config = new PropertiesConfiguration("/dev/application-dev.properties");
8 break;
9 case "prod":
10 config = new PropertiesConfiguration("/prod/application-prod.properties");
11 break;
12 default:
13 config = new PropertiesConfiguration("/dev/application-dev.properties");
14 break;
15 }
16}
通过以上步骤,就可以实现环境的动态切换,运行项目,在项目打印出 当前环境:xxx 就可以判断环境是否切换了。