spring3.1 profile 配置不同的环境
2015-11-30 10:12
706 查看
如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响。
开发时的某些配置比如log4j日志的级别,和生产环境又有所区别。
各种此类的需求,让我希望有一个简单的切换开发环境的好办法,曾经在ROR的时候就很喜欢舒服。
现在spring3.1也给我们带来了profile,可以方便快速的切换环境。
配置环境
使用也是非的方便。只要在applicationContext.xml中添加下边的内容,就可以了
Java代码
<beans profile="develop">
<context:property-placeholder location="classpath*:jdbc-develop.properties"/>
</beans>
<beans profile="production">
<context:property-placeholder location="classpath*:jdbc-production.properties"/>
</beans>
<beans profile="test">
<context:property-placeholder location="classpath*:jdbc-test.properties"/>
</beans>
profile的定义一定要在文档的最下边,否则会有异常。整个xml的结构大概是这样的,
Java代码
<beans xmlns="..." ...>
<bean id="dataSource" ... />
<bean ... />
<beans profile="...">
<bean ...>
</beans>
</beans>
我通过给不同的环境,引入不同的properties来设置不同的属性,你也可以直接在bean里进行定义一些特殊的属性,比如下边这样,在test的时候,初始化数据库与默认数据。(代码摘录:springside)
Java代码
<!-- unit test环境 -->
<beans profile="test">
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/application.properties,
classpath*:/application.test.properties" />
<!-- Simple连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 初始化数据表结构 与默认数据-->
<jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">
<jdbc:script location="classpath:sql/h2/schema.sql" />
<jdbc:script location="classpath:data/import-data.sql" encoding="UTF-8"/>
</jdbc:initialize-database>
</beans>
切换环境
在web.xml中添加一个context-param来切换当前环境:
Java代码
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>develop</param-value>
</context-param>
如果是测试类可以使用注解来切换:
Java代码
@ActiveProfiles("test")
测试类大概是这个样子:
Java代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@ActiveProfiles("test")
public class DictionaryServiceTest extends AbstractTransactionalJUnit4SpringContextTests
你可以写一个基类来写这个注解,然后让你们测试类都继承这个测试基类,会很方便。
开发时的某些配置比如log4j日志的级别,和生产环境又有所区别。
各种此类的需求,让我希望有一个简单的切换开发环境的好办法,曾经在ROR的时候就很喜欢舒服。
现在spring3.1也给我们带来了profile,可以方便快速的切换环境。
配置环境
使用也是非的方便。只要在applicationContext.xml中添加下边的内容,就可以了
Java代码
<beans profile="develop">
<context:property-placeholder location="classpath*:jdbc-develop.properties"/>
</beans>
<beans profile="production">
<context:property-placeholder location="classpath*:jdbc-production.properties"/>
</beans>
<beans profile="test">
<context:property-placeholder location="classpath*:jdbc-test.properties"/>
</beans>
profile的定义一定要在文档的最下边,否则会有异常。整个xml的结构大概是这样的,
Java代码
<beans xmlns="..." ...>
<bean id="dataSource" ... />
<bean ... />
<beans profile="...">
<bean ...>
</beans>
</beans>
我通过给不同的环境,引入不同的properties来设置不同的属性,你也可以直接在bean里进行定义一些特殊的属性,比如下边这样,在test的时候,初始化数据库与默认数据。(代码摘录:springside)
Java代码
<!-- unit test环境 -->
<beans profile="test">
<context:property-placeholder ignore-resource-not-found="true"
location="classpath*:/application.properties,
classpath*:/application.test.properties" />
<!-- Simple连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 初始化数据表结构 与默认数据-->
<jdbc:initialize-database data-source="dataSource" ignore-failures="ALL">
<jdbc:script location="classpath:sql/h2/schema.sql" />
<jdbc:script location="classpath:data/import-data.sql" encoding="UTF-8"/>
</jdbc:initialize-database>
</beans>
切换环境
在web.xml中添加一个context-param来切换当前环境:
Java代码
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>develop</param-value>
</context-param>
如果是测试类可以使用注解来切换:
Java代码
@ActiveProfiles("test")
测试类大概是这个样子:
Java代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@ActiveProfiles("test")
public class DictionaryServiceTest extends AbstractTransactionalJUnit4SpringContextTests
你可以写一个基类来写这个注解,然后让你们测试类都继承这个测试基类,会很方便。
相关文章推荐
- Android之使用Http协议实现文件上传功能
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- mysql集群之MMM简单搭建
- 通晓网络测试常用命令
- Nodejs学习笔记之测试驱动
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- 可以测试javascript运行效果的代码
- 使用php测试硬盘写入速度示例
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- python 测试实现方法
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- MySQL使用profile查询性能的操作教程