zeus持久层spring事务单元测试

今天把zeus事务单元测试放出来,让大家指出他的毛病,

1.ZeusTransactionTest.java 单元测试

package com.dengliang.zeus.webdemo.test;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dengliang.zeus.webdemo.services.IDajc01Service;
import com.dengliang.zeus.webdemo.util.UIDGenerator;
import com.dengliang.zeus.webdemo.vo.Dajc01VO;


//
/**
 * zeus 持久层事务测试 由于打包时要调用 test方法 ,如果测试手工添加@Test
 * @author 100755_邓亮 2015年4月28日
 *
 */
public class ZeusTransactionTest {
	private static ApplicationContext context;
	/**
	 * 获取bean
	 * 
	 * @param id
	 * @return
	 */
	public static Object getBean(String id) {
		if (context == null) {
			synchronized (id) {
				context = new ClassPathXmlApplicationContext(
						"applicationContext-simpledb.xml",
						"applicationContext-dao.xml",
						"applicationContext-service.xml",
						"applicationContext-transaction.xml");
			}
		}
		return context.getBean(id);
	}
	@Test
	public void addTransactionTest() {
		// 新增测试
		IDajc01Service service = (IDajc01Service) ZeusTransactionTest.getBean("dajc01Service");
		try {
			List<Dajc01VO> list=new ArrayList();
			for (int i = 0; i < 10; i++) {
				Dajc01VO da001 = new Dajc01VO();
				da001.setUuid(UIDGenerator.getUUID());
				da001.setSysId(i+"");
				list.add(da001);
			}
			service.addList(list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
}

 2.相关几个配置文件

applicationContext-simpledb.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd 
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd ">

	<context:property-placeholder location="classpath:*.properties" />
	<bean id="parentDataSource" class="com.alibaba.druid.pool.DruidDataSource"
		destroy-method="close">
		<!-- 配置初始化大小、最小、最大 -->
		<property name="initialSize" value="20" />
		<property name="minIdle" value="20" />
		<property name="maxIdle" value="30" />
		<property name="maxActive" value="50" />
		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="0" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="validationQuery" value="SELECT 'x'" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="removeAbandoned" value="false" />
		<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
			<property name="filters" value ="stat,wall"></property>
	 </bean>
     
     <bean id="jlerp_db" parent="parentDataSource" destroy-method="close">
		<property name="url" value="jdbc:oracle:thin:@10.68.2.23:1521/erpdb" />
		<property name="username" value="jlerp_db" />
		<property name="password" value="jlerp_db" />
	</bean>	
	
	<!--daoSupport-->
	<bean id="zeusdaoSupport" class="com.dengliang.zeus.framework.dao.ZeusDaoSupport">
		<property name="dataSource" ref="jlerp_db"/>
	</bean>

</beans>

 applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns:p="http://www.springframework.org/schema/p" 
		xmlns:aop="http://www.springframework.org/schema/aop" 
		xmlns:tx="http://www.springframework.org/schema/tx"
		xmlns:context="http://www.springframework.org/schema/context"  
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
	<!-- DAO -->
	<bean id="dajc01DAO" class="com.dengliang.zeus.webdemo.dao.Dajc01DAO" parent="zeusdaoSupport"/>
</beans>

 applicationContext-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- BIZ -->
	<bean id="dajc01Service" class="com.dengliang.zeus.webdemo.services.impl.Dajc01Service">
		<property name="dajc01DAO" ref="dajc01DAO" />
	</bean>
	
</beans>

 applicationContext-transaction.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context.xsd 
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd ">

	  <bean id = "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="jlerp_db" />
      </bean>
      <!-- 声明式事务配置 -->
	  <tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
				<tx:method name="do*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
			<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
			<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
			<tx:method name="begin*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
			<tx:method name="end*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
			<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
			<tx:method name="modify*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/>
			<tx:method name="query*" propagation="NOT_SUPPORTED"/>
			<tx:method name="load*" propagation="NOT_SUPPORTED"/>
			<tx:method name="find*" propagation="NOT_SUPPORTED"/>
			<tx:method name="*" read-only="true" propagation="SUPPORTS" />
	</tx:attributes>
	</tx:advice>
	<!-- service切面 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.dengliang.zeus.webdemo.services.*.*(..))" id="pointCut" />
		<aop:advisor  pointcut-ref="pointCut" advice-ref="txAdvice"/>
	</aop:config>
   

	


	
</beans>

相关推荐