方法一:system.setProperty,在文中指定
如果ApplicationContext.xml中出现两套beans profile应该如何确定使用是哪套数据库
<!-- production/local development环境 -->
<beans profile="production,development,test1,functional1">
<context:property-placeholder
ignore-unresolvable="true" location="classpath*:/application.properties" />
<!-- 简单data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 初始化数据结构 -->
<jdbc:initialize-database data-source="dataSource"
ignore-failures="ALL">
<jdbc:script location="classpath:sql/h2/schema.sql" />
</jdbc:initialize-database>
</beans>
<!-- unit test环境,functional test环境 -->
<beans profile="test,functional">
<context:property-placeholder
ignore-resource-not-found="true" location="classpath*:/application.test.properties" />
<!-- 简单data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 初始化数据结构 -->
<jdbc:initialize-database data-source="dataSource"
ignore-failures="ALL">
<jdbc:script location="classpath:sql/h2/schema.sql" />
</jdbc:initialize-database>
<!-- 初始化测试数据 -->
<bean class="org.springside.modules.test.data.DataInitializer"
lazy-init="false">
<property name="dataSource" ref="dataSource" />
<property name="dataFile" value="/data/sample-test-data.xml" />
</bean>
</beans>
如上代码:
写单元测试的时候,所有的单元测试用例继承于SpringTransactionalTestCase,这是springside-core-4.0.0.RC3-sources.jar中,注意在类上有标签:@ActiveProfiles("test"),这就指定了他使用的datasource配置是test profile的那一套
/**
* Copyright (c) 2005-2012 springside.org.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
*/
package org.springside.modules.test.spring;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
/**
*
* @author calvin
*/
@ActiveProfiles("test")
public abstract class SpringTransactionalTestCase extends AbstractTransactionalJUnit4SpringContextTests {
protected DataSource dataSource;
@Override
@Autowired
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
this.dataSource = dataSource;
}
}
而在真正的执行方法中使用
System.setProperty("spring.profiles.active", "production");
来指定,使用的dataservice是profile production的那yita
方法二:在web.xml中指定
1.在Spring配置文件applicationContext.xml中定义两组环境
- <beans profile="production">
- <!-- 生成环境 -->
- </beans>
- <beans profile="test">
- <!-- 测试环境 -->
- </beans>
2.在web.xml中,设定使用哪种环境
- <context-param>
- <param-name>spring.profiles.active</param-name>
- <param-value>production</param-value>
- </context-param>