这里写了三种方式
1、 编码式
1、将核心事务管理器配置到spring容器
2.配置TransactionTemplate模板
3.将事务模板注入Service
4.在Service中调用模板
2、XML配置
这里我就直接把代码放这了
先看通知类
package com.hd.springAop;
import org.aopalliance.aop.Advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAdvice implements Advice {
public void abc(){
System.out.println("在调用切点方法前调用");
}
public void after(){
System.out.println("有异常也正调用");
}
public void afterReturning(){
System.out.println("有异常就不调用");
}
public void afterThrowing(){
System.out.println("有异常就调用");
}
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("开启事务");
Object proceed = null;
try {
proceed = joinPoint.proceed();
System.out.println("提交事务");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("回滚事务");
}
return proceed;
}
}
然后是配置
<?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">
<context:component-scan base-package="com.hd.springAop"></context:component-scan>
<!--通知-->
<bean name="myAdvice" class="com.hd.springAop.MyAdvice"></bean>
<!--织入-->
<aop:config>
<!--
void com.hd.springAop.UserServiceImpl.add()
* com.hd.springAop.UserServiceImpl.*()
* com.hd.springAop.*ServiceImpl.*(..)
-->
<!--切点-->
<aop:pointcut id="pt" expression="execution(* com.hd.springAop.*ServiceImpl.*(..))"></aop:pointcut>
<!--切面 :通知+切点-->
<aop:aspect ref="myAdvice" >
<aop:before method="abc" pointcut-ref="pt"></aop:before>
<aop:after-throwing method="afterThrowing" pointcut-ref="pt"></aop:after-throwing>
<aop:after method="after" pointcut-ref="pt"></aop:after>
<aop:after-returning method="afterReturning" pointcut-ref="pt"></aop:after-returning>
<aop:around method="around" pointcut-ref="pt"></aop:around>
</aop:aspect>
</aop:config>
</beans>
要增加的service
package com.hd.springAop;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
@Override
public void add() {
// System.out.println("开启事务");
System.out.println("执行添加");
// System.out.println("回滚事务");
}
@Override
public void delete() {
// int i = 1/0;
// System.out.println("开启事务");
System.out.println("执行删除");
// System.out.println("回滚事务");
}
@Override
public void update() {
// System.out.println("开启事务");
System.out.println("提交事务");
// System.out.println("回滚事务");
}
@Override
public void get() {
System.out.println("开启事务");
System.out.println("提交事务");
System.out.println("回滚事务");
}
}
service接口
package com.hd.springAop;
public interface UserService {
void add();
void delete();
void update();
void get();
}
这就是springaop配置的使用版
还有一种注解模式在这就不写了想看的可以私聊我