spring的环境配置(beans profile="xxx")
2012-01-12 17:06
316 查看
研究spring3的时候发现一个很好用的特性:环境配置(spring2是否有此特性未知)
官方示例代码如下:
这个除了可以切换开发、部署环境,也可以方便地切换不同的数据库。实战中我发现这个TestCase还可以被继承,其配置也会被继承,所以我现在的做法是写一个基础TestCase,配置好ContextConfiguration、ActiveProfiles,其他TestCase继承该基础TestCase。
但是集成到web环境中时,我却很久找不到如何在web.xml中切换这个环境配置,找了很久总算找到了,在web.xml中加入:
经测试已成功,特此记录分享。
官方示例代码如下:
<!-- 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>
经测试已成功,特此记录分享。
相关文章推荐
- Spring3自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- Spring3.1+自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- Spring3自定义环境配置 <beans profile="">
- IntelliJ idea在Spring配置无错误时,装配对象出现 Could not autowire. no beans of "XXX" type found
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 在spring中通过Profile配置不同环境下log配置
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 使用Spring3.1后的的Profile配置使不同环境加载不同配置文件
- 加入spring-boot后导致maven的profile多环境配置失效的问题解决
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- Spring 全注解配置 bean 和 调用 (2) @Profile 自适应不同的模式或环境中
- Spring boot中用Profile配置多个环境参数