本文主要是介绍Spring的AOP面向切面编程针对事务的几种实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、XML纯配置文件实现
配置文件中:
<!--aop配置内容--><aop:config><!--切入点的坐标和来源,是被代理对象的待增强方法--><aop:pointcut id="pcr" expression="execution(* com.fh.service.impl.*.*(..))"/><!--通知类--><aop:aspect ref="transactionManagerAdvice"><!--ref内为通知类(即事务、日志等)的依赖--><aop:before method="beforeMess" pointcut-ref="pcr"/> <!--主业务代码执行前,执行的方法--><aop:after-returning method="afterMessReturn" pointcut-ref="pcr"/><!--主业务代码执行后,正确提交结果时执行的方法--><aop:after-throwing method="afterMessThrow" pointcut-ref="pcr"/><!--主业务代码执行后,出现异常时执行的方法--><aop:after method="afterMess" pointcut-ref="pcr"/><!--主业务代码执行后,最终一定要执行的方法--></aop:aspect></aop:config>
二、注解实现
配置文件中:
//打开aop注解编程的开关
<aop:aspectj-autoproxy/>
代码中:
@Component("transactionManagerAdvice")//Advice 即:通知类的表示
@Aspect//表示所在类为切面
public class TransactionManager {@Autowiredprivate ConnectionUtil connectionUtil;//事务的切入点,该方法无需内部处理,为下面各个操作事务所提供@Pointcut("execution(* com.fh.service.impl.AccountServiceImpl.*(..))")public void pcr(){}//开启事务@Before("pcr()")public void openTransaction(){try {connectionUtil.getConnection().setAutoCommit(false);} catch (SQLException e) {e.printStackTrace();}}//提交事务@AfterReturning("pcr()")public void commitTransaction(){try {connectionUtil.getConnection().commit();} catch (SQLException e) {e.printStackTrace();}}//回滚事务@AfterThrowing("pcr()")public void rollbackTransaction(){try {connectionUtil.getConnection().rollback();} catch (SQLException e) {e.printStackTrace();}}//关闭事务@After("pcr()")public void releaseTransaction(){connectionUtil.release();}
}
需要注意一点,切面注解执行顺序是固定不变的,如下:
try {try {// @Beforemothod.invoke();} finally {// @After}// @AfterReturning
catch() {// @AfterThrowing
}
所以,注解执行事务时,@Before刚刚开启事务,接着 @After就关闭了事务,直接导致@AfterReturning和@AfterThrowing的代码无法运行,因此注解方法无法执行事务,需引入下面的方法——环绕事务。
三、环绕事务实现
配置文件中:
<!--aop配置内容--><aop:config><!--切入点的坐标和来源,是被代理对象的待增强方法--><aop:pointcut id="pcr" expression="execution(* com.fh.service.impl.*.*(..))"/><!--通知类--><aop:aspect ref="transactionManagerAdvice"><!--环绕通知--><aop:around method="aroundMethod" pointcut-ref="pcr"/></aop:aspect></aop:config>
代码中(通知类内):
@Component("transactionManagerAdvice")
@Aspect
public class TransactionManager {@Autowiredprivate ConnectionUtil connectionUtil;//开启事务public void openTransaction(){try {connectionUtil.getConnection().setAutoCommit(false);} catch (SQLException e) {e.printStackTrace();}}//提交事务public void commitTransaction(){try {connectionUtil.getConnection().commit();} catch (SQLException e) {e.printStackTrace();}}//回滚事务public void rollbackTransaction(){try {connectionUtil.getConnection().rollback();} catch (SQLException e) {e.printStackTrace();}}//关闭事务public void releaseTransaction(){connectionUtil.release();}//环绕事务public Object aroundMethod(ProceedingJoinPoint pjp){//ProceedingJoinPoint pjp为被代理对象try {openTransaction();//Object obj = pjp.proceed(pjp.getArgs());//传入被代理对象的参数集,可以为空,得到结果,由于确保了传入值和返回结果,本环绕事务可以应对增删改查等一切方法commitTransaction();return obj;} catch (Throwable throwable) {rollbackTransaction();throw new RuntimeException(throwable);} finally {releaseTransaction();}}
}
四、纯注解(no xml)实现:
在注解的基础上,将配置文件替换为配置类,其类中注解如下所示:
@Configuration//表名本类为spring配置类
@ComponentScan("com.fh")//扫描范围
@EnableAspectJAutoProxy//AOP编程的开关
public class SpringConfig {
}
这篇关于Spring的AOP面向切面编程针对事务的几种实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!