Spring 自定義環境配置

<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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章