前言:
其实 有了 spring boot 之后, 下面的 这么复杂的配置 可以更简单 :
spring xml 的模式:
配置环境
使用也是非的方便。只要在applicationContext.xml中添加下边的内容,就可以了
- <beans profile="develop">
- <context:property-placeholder location="classpath*:jdbc-develop.properties"/>
- </beans>
- <beans profile="production">
- <context:property-placeholder location="classpath*:jdbc-production.properties"/>
- </beans>
- <beans profile="test">
- <context:property-placeholder location="classpath*:jdbc-test.properties"/>
- </beans>
profile的定义一定要在文档的最下边,否则会有异常。整个xml的结构大概是这样的,
- <beans xmlns="..." ...>
- <bean id="dataSource" ... />
- <bean ... />
- <beans profile="...">
- <bean ...>
- </beans>
- </beans>
切换环境
在web.xml中添加一个context-param来切换当前环境:
- <context-param>
- <param-name>spring.profiles.active</param-name>
- <param-value>develop</param-value>
- </context-param>
如果是测试类可以使用注解来切换:
- @ActiveProfiles("test")
测试类
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:applicationContext.xml")
- @ActiveProfiles("test")
- public class DictionaryServiceTest extends AbstractTransactionalJUnit4SpringContextTests