spring--AOP事物处理三种方式

本文深入探讨了Spring框架中三种不同的事务管理方式:基于XML的配置、注解驱动和AspectJ,详细介绍了每种方式的配置步骤及核心概念,如隔离级别、传播机制等。

1、事物处理—SpringAOP配置

applicationContext.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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        				
 		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 			<!-- 设置MyBatis主配置文件的位置 -->
 			<property name="configLocation" value="classpath:mybatisconfig.xml" />
 			<!-- 设置数据源 -->
 			<property name="dataSource" ref="dataSource" />
 		</bean>	
 		<!-- 配置数据源 -->
 		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 			<property name="driverClass" value="${jdbc.driver}" />
			<property name="jdbcUrl" value="${jdbc.url}" />
			<property name="user" value="${jdbc.username}" />
			<property name="password" value="${jdbc.password}" />
 		</bean>
 		<!-- 指定四大参数properties文件的位置 -->
 		<context:property-placeholder location="classpath:jdbc.properties" />
 		<!-- 获取SqlSession
 			获取Dao接口实现类对象  sqlSession.getMapper()
 			MapperScannerConfigurer:自动扫描指定包(dao接口所在的包)
 			自动生成所有dao接口的实现类,对应bean的id:接口是StudentDAO,接口的实现类id是:studentDAO
 		 -->
 		 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		 	<property name="basePackage" value="com.woniu.dao"/>
 		 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 		 </bean>
 		 
 		 <!-- 给service层dao对象赋值 -->
 		 <bean id="studentService" class="com.woniu.service.imp.StudentServiceImp">
 		 	<property name="studentDao" ref="studentDAO"/>
 		 </bean>
 		 
 		 <bean id="accountService" class="com.woniu.service.imp.AccountServiceImp">
 		 	<property name="accountDao" ref="accountDAO"/>
 		 </bean>
 		 
 		 <!-- 配置事物管理器 -->
 		 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 		 	<property name="dataSource"  ref="dataSource"/>
 		 </bean>
 		 
 		 <!-- 配置事物代理对象 -->
 		 <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
 		 	<!-- 配置目标对象 -->
 		 	<property name="target" ref="accountService" />
 		 	<!-- 设置事物管理器 -->
 		 	<property name="transactionManager" ref="transactionManager" />
 		 	<!-- 设置事物的基本属性 -->
 		 	<property name="transactionAttributes">
 		 		<props>
 		 		<!-- key:指定给目标对象中的哪些方法织入事物,支持*通配符 
 		 			value:指定事物的隔离级别和传播机制
 		 			SpringAOP的事物处理,对应两类异常的处理方式不同:
 		 				受查异常(Exception):提交		-Exception(受查异常会回滚)
 		 				非受查异常(runtimeException):回滚       +RuntiemException(非受查异常提交,无意义)
 		 			-->
 		 			<prop key="change*">ISOLATION_DEFAULT,PROPAGATION_REQUIRED,-Exception</prop>
 		 		</props>
 		 	</property>
 		 </bean>
</beans>

2、事务处理—SpirngAOP注解

applicationContext.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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
 				
 		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 			<!-- 设置MyBatis主配置文件的位置 -->
 			<property name="configLocation" value="classpath:mybatisconfig.xml" />
 			<!-- 设置数据源 -->
 			<property name="dataSource" ref="dataSource" />
 		</bean>	
 		<!-- 配置数据源 -->
 		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 			<property name="driverClass" value="${jdbc.driver}" />
			<property name="jdbcUrl" value="${jdbc.url}" />
			<property name="user" value="${jdbc.username}" />
			<property name="password" value="${jdbc.password}" />
 		</bean>
 		<!-- 指定四大参数properties文件的位置 -->
 		<context:property-placeholder location="classpath:jdbc.properties" />
 		<!-- 获取SqlSession
 			获取Dao接口实现类对象  sqlSession.getMapper()
 			MapperScannerConfigurer:自动扫描指定包(dao接口所在的包)
 			自动生成所有dao接口的实现类,对应bean的id:接口是StudentDAO,接口的实现类id是:studentDAO
 		 -->
 		 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		 	<property name="basePackage" value="com.woniu.dao"/>
 		 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 		 </bean>
 		 
 		 <!-- 给service层dao对象赋值 -->
 		 <bean id="studentService" class="com.woniu.service.imp.StudentServiceImp">
 		 	<property name="studentDao" ref="studentDAO"/>
 		 </bean>
 		 
 		 <bean id="accountService" class="com.woniu.service.imp.AccountServiceImp">
 		 	<property name="accountDao" ref="accountDAO"/>
 		 </bean>
 		 
 		 <!-- 配置事物管理器 -->
 		 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 		 	<property name="dataSource"  ref="dataSource"/>
 		 </bean>
 		 
 		 <!-- 配置事物注解驱动 -->
 		 <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

service实现类:

public class AccountServiceImp implements AccountService{
	AccountDAO  accountDao;	
	public void setAccountDao(AccountDAO accountDao) {
		this.accountDao = accountDao;
	}
	@Override
    //事物处理注解
    //isolation:隔离级别
    //propagation:传播机制
    //rollbackFor:事物回滚
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public void changeAccount(String from, String to, double balance) throws Exception {
			Double f = accountDao.selectBalance(from);
			Double t = accountDao.selectBalance(to);
			if(f<balance) {
				System.out.println("余额不足!");
				return;
			}
			Double balance1 = f-balance;
			accountDao.updataAccount(from, balance1);
			if(true) {
				throw new Exception("转账异常");
			}
			Double balance2 = t+balance;
			accountDao.updataAccount(to, balance2);
			System.out.println("转账成功");
	}
}

3、事务处理—SpirngAspectJ配置

applicationContext.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:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:aop="http://www.springframework.org/schema/aop" 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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        
 				
 		<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 			<!-- 设置MyBatis主配置文件的位置 -->
 			<property name="configLocation" value="classpath:mybatisconfig.xml" />
 			<!-- 设置数据源 -->
 			<property name="dataSource" ref="dataSource" />
 		</bean>	
 		<!-- 配置数据源 -->
 		<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 			<property name="driverClass" value="${jdbc.driver}" />
			<property name="jdbcUrl" value="${jdbc.url}" />
			<property name="user" value="${jdbc.username}" />
			<property name="password" value="${jdbc.password}" />
 		</bean>
 		<!-- 指定四大参数properties文件的位置 -->
 		<context:property-placeholder location="classpath:jdbc.properties" />
 		<!-- 获取SqlSession
 			获取Dao接口实现类对象  sqlSession.getMapper()
 			MapperScannerConfigurer:自动扫描指定包(dao接口所在的包)
 			自动生成所有dao接口的实现类,对应bean的id:接口是StudentDAO,接口的实现类id是:studentDAO
 		 -->
 		 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		 	<property name="basePackage" value="com.woniu.dao"/>
 		 	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
 		 </bean>
 		 
 		 <!-- 给service层dao对象赋值 -->
 		 <bean id="studentService" class="com.woniu.service.imp.StudentServiceImp">
 		 	<property name="studentDao" ref="studentDAO"/>
 		 </bean>
 		 
 		 <bean id="accountService" class="com.woniu.service.imp.AccountServiceImp">
 		 	<property name="accountDao" ref="accountDAO"/>
 		 </bean>
 		 
 		 <!-- 配置事物管理器 -->
 		 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 		 	<property name="dataSource"  ref="dataSource"/>
 		 </bean>
 		 <!-- 配置事物通知 -->
 		 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 		 	<tx:attributes>
 		 		<tx:method name="change*" isolation="DEFAULT" propagation="REQUIRED" rollback-for="Exception"/>
 		 	</tx:attributes>
 		 </tx:advice>
 		 
 		 <aop:config>
 		 	<aop:pointcut expression="execution(* *..service.*.*(..))"  id="txpointcut"/>
 		 	<!-- 顾问 -->
 		 	<aop:advisor advice-ref="txAdvice" pointcut-ref="txpointcut" />
 		 </aop:config>
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值