经过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换

软件开发的通常流程为工程师开发 -> 测试 -> 上线,所以就涉及到三个不一样的环境,开发环境、测试环境以及生产环境,一般这三个环境会有不少配置参数不一样,例如数据源、文件路径、url等,若是每次上线一个新版本时都手动修改配置会十分繁琐,容易出错。spring 为咱们提供了 profile 机制来解决这个问题。


spring容许咱们经过定义 profile 来将若干不一样的 bean 定义组织起来,从而实现不一样环境自动激活不一样的 profile 来切换配置参数的功能,下面介绍以 xml 的方式定义 profile、如何激活 profile以及定义默认的 profile,整个过程我以配置不一样环境的数据源为例,为了简化配置,这里假设只有开发和生产两个环境。


数据源定义为

[html]  view plain  copy
  1. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  2.     <property name="user" value="${jdbc.user}" />   
  3.     <property name="password" value="${jdbc.password}" />   
  4.     <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />     
  5.     <property name="driverClass" value="${jdbc.driverClass}" />  
  6.     <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>  
  7.     <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>  
  8.     <property name="minPoolSize" value="${c3p0.minPoolSize}"/>  
  9.     <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>  
  10.     <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />  
  11.     <property name="preferredTestQuery" value="${c3p0.preferredTestQuery}"/>  
  12. </bean>  




classpath下外部资源文件有两个 settings-development.properties 和 settings-production.properties,分别是开发环境和生产环境的数据源配置参数,内容以下


settings-development.properties
[html]  view plain  copy
  1. jdbc.user=root  
  2. jdbc.password=111111  
  3. jdbc.driverClass=com.mysql.jdbc.Driver  
  4. jdbc.jdbcUrl=jdbc:mysql://localhost:3306/xxx  
  5. c3p0.minPoolSize=5   
  6. c3p0.initialPoolSize=5  
  7. c3p0.acquireIncrement=5  
  8. c3p0.maxIdleTime=3600  
  9. c3p0.idleConnectionTestPeriod=3600  
  10. c3p0.preferredTestQuery=select 1  



settings-production.properties
[html]  view plain  copy
  1. jdbc.user=xxx  
  2. jdbc.password=xxxx  
  3. jdbc.driverClass=com.mysql.jdbc.Driver  
  4. jdbc.jdbcUrl=jdbc:mysql:///xxx  
  5. c3p0.minPoolSize=20   
  6. c3p0.initialPoolSize=20  
  7. c3p0.acquireIncrement=10  
  8. c3p0.maxIdleTime=3600  
  9. c3p0.idleConnectionTestPeriod=3600  
  10. c3p0.preferredTestQuery=select 1  





1. 定义 profile


如今就能够经过定义 profile 来将开发和生产环境的数据源配置分开,这里咱们定义两个 profile,一个名称是 development,另外一个名称是 production
[html]  view plain  copy
  1. <!-- 开发环境配置文件 -->  
  2. <beans profile="development">  
  3.     <context:property-placeholder location="classpath:settings-development.properties"/>  
  4. </beans>  
  5.    
  6. <!-- 生产环境配置文件 -->  
  7. <beans profile="production">  
  8.     <context:property-placeholder location="classpath:settings-production.properties"/>  
  9. </beans>  





2. 定义默认 profile


默认 profile 是指在没有任何 profile 被激活的状况下,默认 profile 内定义的内容将被使用,一般能够在 web.xml 中定义全局 servlet 上下文参数 spring.profiles.default 实现,代码以下

[html]  view plain  copy
  1. <!-- 配置spring的默认profile -->  
  2. <context-param>  
  3.     <param-name>spring.profiles.default</param-name>  
  4.     <param-value>development</param-value>  
  5. </context-param>  




3. 激活 profile 


spring 为咱们提供了大量的激活 profile 的方法,能够经过代码来激活,也能够经过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile,这里咱们经过定义 JVM 参数实现。
在生产环境中,以 tomcat 为例,咱们在 tomcat 的启动脚本中加入如下 JVM 参数

[html]  view plain  copy
  1. -Dspring.profiles.active="production"
而开发环境中不须要定义该参数,若是不定义,则会使用咱们指定的默认 profile ,在这里也就是名称为 development 的 profile。这样当项目部署到不一样的环境时,即可以经过 JVM 参数来实现不一样环境 profile 自动激活。 4. 延伸 该参数还能够延伸,以致于咱们能够在 java 代码中继续经过该参数来区分不一样的环境,从而执行不一样的操做,代码以下 public class Config {       public static String ENV = "development"; } public class InitConfigListener implements ServletContextListener {     public void contextInitialized(ServletContextEvent sce) {         //侦测jvm环境,并缓存到全局变量中         String env = System.getProperty("spring.profiles.active");         if(env == null) {             Config.ENV = "development";         } else {             Config.ENV = env;         }     } } 在项目初始化时获取到该参数后,咱们即可以在项目任何位置使用,来执行一些不一样环境须要区别对待的操做,例如统计流量的代码只须要在生产环境激活,就能够在jsp中加入以下代码 <!-- 生产环境统计、推送代码 --> <c:if test="${env == 'production' }"> <script> //统计代码 .. </script> </c:if>
相关文章
相关标签/搜索