qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

项目开发中切换部署开发、测试、生产多环境

在开发的过程中,不可避免会接触到至少三个环境的程序部署:开发、测试和生产环境。
  每个环境都使用一套数据库配置,路径配置等,如果每次都人工的干预每一个配置文件,工作会比较繁杂,且容易遗漏并且出错。
  spring3.1之后提供了profile功能,可以切换不同的自定义profile环境,唯一的缺点是和maven结合不大好,只能在web.xml中进行修改。
  方法如下:
  1、在beans.xml中定义各个环境。
<beans profile="develop">
</beans>
<beans profile="test">
</beans>
<beans profile="product">
</beans>
  每个环境如果使用了不同的配置文件(properties文件等)可以在环境中进行加载声明。
  该段代码需在文件根节点的最后一段
  如
  <beans profile="test">
  <context:property-placeholder location="classpath*:jdbc-test.properties"/>
  </beans>
  2、定义属性之外的配置,如指定数据库bean等
<beans profile="test,develop">
<bean id="authDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@208.120.102.10:1522:ora11g">
</property>
<property name="username" value="user"></property>
<property name="password" value="passwd"></property>
</bean>
</beans>
<beans profile="product">
<bean id="authDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@198.121.33.7:1521:ora10g">
</property>
<property name="username" value="user"></property>
<property name="password" value="passwd"></property>
</bean>
</beans>
此处定义了不同环境下不同的数据库链接信息
  3、web.xml中定义当前使用哪个环境
  在web.xml中操作context-param节点
  <context-param>
  <param-name>spring.profiles.active</param-name>
  <param-value>product</param-value>
  </context-param>
  部署时指定哪个环境为激活状态即可。
  如果进行junit测试可以使用
  @ActiveProfiles({"test","develop"})
  附: 如果spring的profile可以和maven的发布共同作用就更好了,但是笔者目前还未能成功将2者结合。
  配置提醒:
  <beans xmlns="http://www.springframework.org/schema/beans" profile="test,develop" -----设置这个之后,数据库只对test,develop有效
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
  <!-- 定义数据连接池 -->
  <!-- 使用spring自带的DriverManagerDataSource方式 -->

posted on 2014-06-13 09:56 顺其自然EVO 阅读(548) 评论(1)  编辑  收藏

评论

# re: 项目开发中切换部署开发、测试、生产多环境 2014-06-18 10:59 yongxin

maven也有各种环境配置的,利用resource的filters  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜