Spring环境配置
来源:互联网 发布:锵锵软件官网 编辑:程序博客网 时间:2024/06/05 07:38
研究spring3的时候发现一个很好用的特性:环境配置(spring2是否有此特性未知)
官方示例代码如下:
<!-- app-config.xml --><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="..."> <bean id="transferService" class="com.bank.service.internal.DefaultTransferService"> <constructor-arg ref="accountRepository" /> <constructor-arg ref="feePolicy" /> </bean> <bean id="accountRepository" class="com.bank.repository.internal.JdbcAccountRepository"> <constructor-arg ref="dataSource" /> </bean> <bean id="feePolicy" class="com.bank.service.internal.ZeroFeePolicy" /> <beans profile="dev"> <jdbc:embedded-database id="dataSource"> <jdbc:script location="classpath:com/bank/config/sql/schema.sql" /> <jdbc:script location="classpath:com/bank/config/sql/test-data.sql" /> </jdbc:embedded-database> </beans> <beans profile="production"> <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/datasource" /> </beans></beans>
package com.bank.service;@RunWith(SpringJUnit4ClassRunner.class)// ApplicationContext will be loaded from "classpath:/app-config.xml"@ContextConfiguration("/app-config.xml")@ActiveProfiles("dev")public class TransferServiceTest { @Autowired private TransferService transferService; @Test public void testTransferService() { // test the transferService }}
这个除了可以切换开发、部署环境,也可以方便地切换不同的数据库。实战中我发现这个TestCase还可以被继承,其配置也会被继承,所以我现在的做法是写一个基础TestCase,配置好ContextConfiguration、ActiveProfiles,其他TestCase继承该基础TestCase。
但是集成到web环境中时,我却很久找不到如何在web.xml中切换这个环境配置,找了很久总算找到了,在web.xml中加入:
<context-param> <param-name>spring.profiles.active</param-name> <param-value>dev</param-value></context-param>
0 0
- spring环境配置实例
- Spring环境配置
- spring的环境配置
- Spring环境配置
- Spring 环境安装配置
- Spring 环境配置
- Spring--环境配置
- Spring多环境配置
- Spring入门---环境配置
- spring 环境配置
- Spring 配置多环境
- spring环境配置
- spring环境配置
- Spring环境配置
- Spring 自定义环境配置
- eclipse开发spring环境配置
- Spring的环境配置问题
- Spring+Hibernate+Struts环境配置
- ScrollView分析
- 《Java并发编程实战》---线程安全性---对象的共享
- 推荐系统挖掘长尾了吗?
- asp.net中FTP读取文件列表时中文乱码
- ipad,iphone启动图片命名
- Spring环境配置
- 《数据结构》实验二: 线性表实验
- c语言文件操作函数
- Oracle联机日志文件损坏后的恢复方法
- VC窗口形状的绘制---SetWindowRgn
- 第十二周 项目2:(1)N组数的最大公约数
- 如何强制旋转屏幕
- 特性的使用
- 硅谷大佬内部发言:千亿美金公司,最初的真相