ApplicationContext中profile指定解读

方法一: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中定义两组环境 

Java代码   收藏代码
  1. <beans profile="production">  
  2.     <!-- 生成环境 -->  
  3. </beans>  
  4.   
  5. <beans profile="test">  
  6.     <!-- 测试环境 -->  
  7. </beans>  


2.在web.xml中,设定使用哪种环境 
Java代码   收藏代码
  1. <context-param>  
  2.     <param-name>spring.profiles.active</param-name>  
  3.     <param-value>production</param-value>  
  4. </context-param>  


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值