spring profiles

前言:

     其实  有了 spring boot 之后,  下面的 这么复杂的配置  可以更简单 :



spring xml 的模式:

配置环境

使用也是非的方便。只要在applicationContext.xml中添加下边的内容,就可以了

 

Java代码   收藏代码
  1. <beans profile="develop">  
  2.         <context:property-placeholder location="classpath*:jdbc-develop.properties"/>  
  3.     </beans>  
  4.     <beans profile="production">  
  5.         <context:property-placeholder location="classpath*:jdbc-production.properties"/>  
  6.     </beans>  
  7.     <beans profile="test">  
  8.         <context:property-placeholder location="classpath*:jdbc-test.properties"/>  
  9.     </beans>  

profile的定义一定要在文档的最下边,否则会有异常。整个xml的结构大概是这样的,

   

Java代码   收藏代码
  1. <beans xmlns="..." ...>  
  2.   <bean id="dataSource" ... />  
  3.   <bean ... />  
  4.   <beans profile="...">  
  5.     <bean ...>  
  6.   </beans>  
  7. </beans>  

 切换环境

  在web.xml中添加一个context-param来切换当前环境:

Java代码   收藏代码
  1. <context-param>  
  2.     <param-name>spring.profiles.active</param-name>  
  3.     <param-value>develop</param-value>  
  4. </context-param>  

 如果是测试类可以使用注解来切换:

Java代码   收藏代码
  1. @ActiveProfiles("test")  

 测试类

Java代码   收藏代码
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = "classpath:applicationContext.xml")  
  3. @ActiveProfiles("test")  
  4. public class DictionaryServiceTest extends AbstractTransactionalJUnit4SpringContextTests  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值