Spring事务控制AOP环绕切入底层原理

前面我们对Spring事务框架以及Spring启用事务管理的@EnableTransactionManagement进行了学习,了解了Spring事务管理的底层工作机制。

有关Spring事务管理剩余尚未学习研究的还有事务如何生效、以及事务生效后具体的AOP切入和增强过程

我们先研究事务生效后具体的AOP切入和增强过程。

基于Cglib实现的AOP增强都是通过拦截器Interceptor实现的,通过前面几篇文章的分析,我们知道Spring事务的拦截器是TransactionInteceptor,我们直接从拦截器的invoke方法入手。

TransactionInteceptor#invoke

看一下TransactionInterceptor的类结构:

AOP Alliance MethodInterceptor for declarative transaction management using the common Spring transaction infrastructure (PlatformTransactionManager/ org.springframework.transaction.ReactiveTransactionManager).
Derives from the TransactionAspectSupport class which contains the integration with Spring's underlying transaction API. TransactionInterceptor simply calls the relevant superclass methods such as invokeWithinTransaction in the correct order.

AOP联盟的方法拦截器,用于实现基于Spring事务框架的事务管理功能。
继承自集成了Spring底层事务管理API的TransactionAspectSupport类。

所以通过JavaDoc我们知道TransactionIntecepor是Spring事务管理中的方法拦截器,他有一个父类叫TransactionAspectSupport。

开始分析他的invoke方法。

首先获取到targetClass,targetClass其实就是被@Transactional注解的业务类,通过参数invocation传进来。

@Override
    @Nullable
    public Object invoke(MethodInvocation invocation) throws Throwable {
        // Work out the target class: may be {@code null}.
        // The TransactionAttributeSource should be passed the target class
        // as well as the method, which may be from an interface.
        Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);

然后调用invokeWithinTransaction方法,该方法在TransactionAspectSupport类实现,我们稍后分析。

// Adapt to TransactionAspectSupport's invokeWithinTransaction...
        return invokeWithinTransaction(invocation.getMethod(), targetClass, new CoroutinesInvocationCallback() {
            @Override
            @Nullable
            public Object proceedWithInvocation() throws Throwable {
                return invocation.proceed();
            }
            @Override
            public Object getTarget() {
                return invocation.getThis();
            }
            @Override
            public Object[] getArguments() {
                return invocation.getArguments();
            }
        });
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值