软件开发的通常流程为工程师开发 -> 测试 -> 上线,所以就涉及到三个不一样的环境,开发环境、测试环境以及生产环境,一般这三个环境会有不少配置参数不一样,例如数据源、文件路径、url等,若是每次上线一个新版本时都手动修改配置会十分繁琐,容易出错。spring 为咱们提供了 profile 机制来解决这个问题。html
spring容许咱们经过定义 profile 来将若干不一样的 bean 定义组织起来,从而实现不一样环境自动激活不一样的 profile 来切换配置参数的功能,下面介绍以 xml 的方式定义 profile、如何激活 profile以及定义默认的 profile,整个过程我以配置不一样环境的数据源为例,为了简化配置,这里假设只有开发和生产两个环境。java
数据源定义为mysql
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="initialPoolSize" value="${c3p0.initialPoolSize}"/> <property name="acquireIncrement" value="${c3p0.acquireIncrement}"/> <property name="minPoolSize" value="${c3p0.minPoolSize}"/> <property name="maxIdleTime" value="${c3p0.maxIdleTime}"/> <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" /> <property name="preferredTestQuery" value="${c3p0.preferredTestQuery}"/> </bean>
settings-development.propertiesweb
jdbc.user=root jdbc.password=111111 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/xxx c3p0.minPoolSize=5 c3p0.initialPoolSize=5 c3p0.acquireIncrement=5 c3p0.maxIdleTime=3600 c3p0.idleConnectionTestPeriod=3600 c3p0.preferredTestQuery=select 1
jdbc.user=xxx jdbc.password=xxxx jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///xxx c3p0.minPoolSize=20 c3p0.initialPoolSize=20 c3p0.acquireIncrement=10 c3p0.maxIdleTime=3600 c3p0.idleConnectionTestPeriod=3600 c3p0.preferredTestQuery=select 1
如今就能够经过定义 profile 来将开发和生产环境的数据源配置分开,这里咱们定义两个 profile,一个名称是 development,另外一个名称是 production(配置到Spring的配置文件中便可)spring
<!-- 开发环境配置文件 --> <beans profile="development"> <context:property-placeholder location="classpath:settings-development.properties"/> </beans> <!-- 生产环境配置文件 --> <beans profile="production"> <context:property-placeholder location="classpath:settings-production.properties"/> </beans>
默认 profile 是指在没有任何 profile 被激活的状况下,默认 profile 内定义的内容将被使用,一般能够在 web.xml 中定义全局 servlet 上下文参数 spring.profiles.default 实现,代码以下sql
<!-- 配置spring的默认profile --> <context-param> <param-name>spring.profiles.default</param-name> <param-value>development</param-value> </context-param>
固然此种方式也是使用默认profile的方式,即若是不指定激活哪一个环境则使用默认方式加载文件缓存
<beans profile="default"> <context:property-placeholder ignore-resource-not-found="true" location="classpath:application.properties"/> </beans>
spring 为咱们提供了大量的激活 profile 的方法,能够经过代码来激活,也能够经过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile,这里咱们经过定义 JVM 参数实现。tomcat
在生产环境中,以 tomcat 为例,咱们在 tomcat 的启动脚本中加入如下 JVM 参数bash
1
|
-Dspring.profiles.active=
"production"
|
而开发环境中不须要定义该参数,若是不定义,则会使用咱们指定的默认 profile ,在这里也就是名称为 development 的 profile。这样当项目部署到不一样的环境时,即可以经过 JVM 参数来实现不一样环境 profile 自动激活。app
该参数还能够延伸,以致于咱们能够在 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; } } }
<!-- 生产环境统计、推送代码 --> <c:if test="${env == 'production' }"> <script> //统计代码 .. </script> </c:if>
原文地址:http://blog.lifw.org/post/68990012