如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响。
开发时的某些配置比如log4j日志的级别,和生产环境又有所区别。
各种此类的需求,让我希望有一个简单的切换开发环境的好办法,曾经在ROR的时候就很喜欢舒服。
现在spring3.1也给我们带来了profile,可以方便快速的切换环境。
配置环境
使用也是非的方便。只要在applicationContext.xml中添加下边的内容,就可以了
<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的结构大概是这样的,
<beans xmlns="..." ...> <bean id="dataSource" ... /> <bean ... /> <beans profile="..."> <bean ...> </beans> </beans>
我通过给不同的环境,引入不同的properties来设置不同的属性,你也可以直接在bean里进行定义一些特殊的属性,比如下边这样,在test的时候,初始化数据库与默认数据。(代码摘录:springside)
<!-- 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来切换当前环境:
<context-param> <param-name>spring.profiles.active</param-name> <param-value>develop</param-value> </context-param>
如果是测试类可以使用注解来切换:
@ActiveProfiles("test")
测试类大概是这个样子:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") @ActiveProfiles("test") public class DictionaryServiceTest extends AbstractTransactionalJUnit4SpringContextTests
你可以写一个基类来写这个注解,然后让你们测试类都继承这个测试基类,会很方便。
相关推荐
3.1 覆盖Spring Boot自动配置 42 3.1.1 保护应用程序 43 3.1.2 创建自定义的安全配置 44 3.1.3 掀开自动配置的神 秘面纱 48 3.2 通过属性文件外置配置 49 3.2.1 自动配置微调 50 3.2.2 应用程序Bean的...
以减少不同环境下,配置文件切换的需要 ${JAVA_HOME}/com/bank/service/${env}-config.xml"/> 5、xml的什么,不感兴趣 6、hibernate4支持,不感兴趣 7、spring测试框架和2,3,4的结合 8、spring配置文件中namespace...
3.1 覆盖Spring Boot自动配置 42 3.1.1 保护应用程序 43 3.1.2 创建自定义的安全配置 44 3.1.3 掀开自动配置的神秘面纱 48 3.2 通过属性文件外置配置 49 3.2.1 自动配置微调 50 3.2.2 应用程序...
2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 properties配置文件乱码问题 24 2.5 @ConfigurationProperties与@Value的区别 25 2.6 配置@PropertySource、@ImportResource、@...
3.1 Spring Aware 48 3.1.1 点睛 48 3.1.2 示例 49 3.2 多线程 51 3.2.1 点睛 51 3.2.2 示例 51 3.3 计划任务 54 3.3.1 点睛 54 3.3.2 示例 54 3.4 条件注解@Conditional 56 3.4.1 点睛 56 3.4.2 示例 57 3.5 组合...
1.4.1. What was new in Spring 3.1? 1.4.2. What was new in Spring 3.2? 1.4.3. What’s new in Spring 4.0? 1.5. Summary Chapter 2. Wiring beans 2.1. Exploring Spring’s configuration options 2.2. ...
3.1 Spring Aware 48 3.1.1 点睛 48 3.1.2 示例 49 3.2 多线程 51 3.2.1 点睛 51 3.2.2 示例 51 3.3 计划任务 54 3.3.1 点睛 54 3.3.2 示例 54 3.4 条件注解@Conditional 56 3.4.1 点睛 56 3.4.2 示例 57 3.5 组合...
3.1 Spring Aware 48 3.1.1 点睛 48 3.1.2 示例 49 3.2 多线程 51 3.2.1 点睛 51 3.2.2 示例 51 3.3 计划任务 54 3.3.1 点睛 54 3.3.2 示例 54 3.4 条件注解@Conditional 56 3.4.1 点睛 56 3.4.2 示例 57 3.5 组合...
Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...
3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解与元...
3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解与元...
48 3.1 Spring Aware .... ..................................... 48 3.2 多线程 .... .... ......... 51 3.3 计划任务 .... .... ..... 54 3.4 条件注解@Conditional .... .................... 56 3.5 组合注解...
在Spring环境中使用YAML暴露属性 iii. 23.6.3. Multi-profile YAML文档 iv. 23.6.4. YAML缺点 vii. 23.7. 类型安全的配置属性 i. 23.7.1. 第三方配置 ii. 23.7.2. 松散的绑定(Relaxed binding) iii. 23.7.3. @...
3.1 覆盖 Spring Boot 自动配置 ..................... 42 3.1.1 保护应用程序 .............................. 43 3.1.2 创建自定义的安全配置 ............... 44 3.1.3 掀开自动配置的神秘面纱 ........... 48 ...
4. 扩展与高级配置 4.1 修改项目名和包名 4.1.1 修改项目名 4.1.2 修改包名 4.2 放过接口权限验证 4.3 静态资源和模板位置的变更 4.4 三个或更多数据源如何配置 4.5 添加登录验证码 4.6 spring profile 4.7 多机器...
156 Spring Boot 启用 Profile 157 Docker 私服搭建 158 项目的容器化部署1 159 项目的容器化部署2.1 159 项目的容器化部署2.2 160 项目的容器化部署3.1 160 项目的容器化部署3.2 161 基本概念 162 操作流程 163 ...
3.1. 简介 3.1.1. 下载本章的例子 3.2. 创建一个简单的项目 3.3. 构建一个简单的项目 3.4. 简单的项目对象模型 (Project Object Model) 3.5. 核心概念 3.5.1. Maven插件和目标 (Plugins and Goals) 3.5.2...
156 Spring Boot 启用 Profile 157 Docker 私服搭建 158 项目的容器化部署1 159 项目的容器化部署2.1 159 项目的容器化部署2.2 160 项目的容器化部署3.1 160 项目的容器化部署3.2 161 基本概念 162 操作流程...
高级使用手册 4.1 修改项目名和包名 4.2 在线参数配置 4.3 模板位置的变更 4.4 顶部广告的关闭 4.5 添加登录验证码 4.6 spring profile 4.7 多机器部署注意事项 4.8 XSS过滤器 4.8.1 介绍 4.8.2 原理 4.8.3 放过...
3.1 覆盖 Spring Boot 自动配置 ..................... 42 3.1.1 保护应用程序 .............................. 43 3.1.2 创建自定义的安全配置 ............... 44 3.1.3 掀开自动配置的神秘面纱 ........... 48 ...