Spring 自定义环境配置

来源:互联网 发布:in是什么交流软件 编辑:程序博客网 时间:2024/06/01 18:21
<beans profile="test">.......</beans>   <beans profile="production">.......</beans>

1.在用到ApplicationContext的单元测试用例中,用 @ActiveProfiles

@ContextConfiguration(locations = { "/applicationContext.xml" })  @ActiveProfiles("test")

2.在development和functional test启动Jetty前设置系统变量

System.setProperty("spring.profiles.active", "development");   server.start()

3.在web.xml里

<context-param>  <param-name>spring.profiles.default</param-name>  <param-value>production</param-value>  </context-param>

4.在非生产环境,可以用系统变量”spring.profiles.active”进行覆盖

Run As -->Run Configurations -->Environment 设置变量:spring.profiles.active = development

5.采用Spring框架的独立应用程序

GenericXmlApplicationContext ctx=new GenericXmlApplicationContext();ctx.getEnvironment().setActiveProfiles("test");ctx.load("classpath:applicationContext.xml");ctx.refresh();
原创粉丝点击