为了方便讲解,这里的A、B、C类都是Spring管理的Bean。

自调用行为

自调用行为示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
​
@Component
public class B {
    @Autowired
    public void b() {
        this.c();
    }
    
    @Transactional
    public void c() {
​
    }
}

不生效的原因

代码示例 B.b() 直接调用 B.c() 是一个典型的自调用情况。在这种情况下,即使 B.c() 方法有 @Transactional 注解,由于是在同一个类的内部直接调用,事务注解不会被 Spring 的代理机制捕获,因此不会生效。

在 Spring 的 AOP(面向切面编程)架构中,事务管理是通过代理(Proxy)来实现的。当一个方法被调用时,如果这个方法从外部(即通过 Spring 容器管理的 Bean)调用,并且配置了 @Transactional,Spring 会通过代理来拦截这个调用,然后根据 @Transactional 的属性来开始、加入、挂起或结束事务。但如果这个调用是从同一个类的内部发生的,比如一个方法直接调用同一个类中的另一个方法,这种调用就不会经过代理,Spring 无法施加事务管理的逻辑。

自调用行为解决方案

通过 Spring ApplicationContext 获取代理

可以让 Spring 注入自身的 ApplicationContext,并使用它来获取当前 Bean 的代理,然后通过代理来调用方法。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
​
@Component
public class B {
    @Autowired
    private ApplicationContext applicationContext;
​
    public void b() {
        B b = applicationContext.getBean(B.class);
        b.c();
    }
​
    public void c() {
        // Transactional code
    }
}
使用 AopContext.currentProxy()

这是一个更简单的方式,但需要在 Spring 配置中启用 exposeProxy=true。然后你可以通过 AopContext.currentProxy() 来获取当前对象的代理并进行调用。

要启用 exposeProxy,需要在 Spring 的配置中设置:

@EnableAspectJAutoProxy(exposeProxy = true)
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
​
@Component
public class B {
    public void b() {
        ((B) AopContext.currentProxy()).c();
    }
​
    @Transactional
    public void c() {
        // Transactional code
    }
}

正确的事务传播行为

2. 创建三个Bean类

接下来,我们创建三个类,每个类都有不同的方法,演示事务的传播:

A类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
​
@Service
public class A {
    @Autowired
    private B b;
​
    @Transactional
    public void methodA() {
        System.out.println("Inside A.methodA");
        b.methodB();
    }
}
B类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
​
@Service
public class B {
    @Autowired
    private C c;
​
    @Transactional
    public void methodB() {
        System.out.println("Inside B.methodB");
        c.methodC();
    }
}
C类
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
​
@Service
public class C {
​
    @Transactional
    public void methodC() {
        System.out.println("Inside C.methodC");
        // 这里可以添加实际的数据库操作代码
    }
}

解释

在这个设置中:

  • 当 methodA 被调用时,它在 A 类的事务环境中开始执行。

  • A.methodA 调用 B.methodB,B.methodB 进一步调用 C.methodC。由于每个方法都通过Spring的代理调用,每个方法都有 @Transactional 注解,这保证了事务在整个调用链中得以正确管理。

  • 如果有需要,每个方法可以根据其事务传播设置独立地控制事务行为。在这个示例中,我们没有明确设置传播行为,所以它们默认使用 Propagation.REQUIRED,这意味着它们会加入到现有的事务中。

为什么这里的methodB没有明写事务,但是methodA、methodB、methodC都有事务

因为A.methodA事务传播到B.methodB,B.methodB传播到C.methodC,因为B.methodB调用C.methodC不属于自调用,所以Spring能捕获到并且进行添加事务。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部