程序员Spring 开发SpringFramework

[Spring] 又一次事务不生效的排查

2018-12-19  本文已影响3人  殷天文

上一篇关于事务的 [解决] spring service 调用当前类方法事务不生效

正文

今天在工作中突然发现标记了 @Transactional 的方法,事务并没有生效。在检查了业务层是否 try catch异常,MySQL存储引擎之后。心态已近崩溃的边缘:)
这个时候突然发现了一个神奇的现象!


两个@Service 都标记了 @Transactional,userService 并没有生成代理对象,也就导致了事务不生效。

继续排查,百度追凶,最后锁定了凶手 --- BeanPostProcessor 先看一下官方描述

/**
 * Factory hook that allows for custom modification of new bean instances,
 * e.g. checking for marker interfaces or wrapping them with proxies.
 *
 * <p>ApplicationContexts can autodetect BeanPostProcessor beans in their
 * bean definitions and apply them to any beans subsequently created.
 * Plain bean factories allow for programmatic registration of post-processors,
 * applying to all beans created through this factory.
 *
 * <p>Typically, post-processors that populate beans via marker interfaces
 * or the like will implement {@link #postProcessBeforeInitialization},
 * while post-processors that wrap beans with proxies will normally
 * implement {@link #postProcessAfterInitialization}.
 *

简单的来说,BeanPostProcessor是Spring 给我们提供的一个扩展接口

public interface BeanPostProcessor {
    // bean实例化方法调用前被调用
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
    // bean实例化方法调用后被调用
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

}
分析:
解决方案:
public class AuthRealm extends AuthorizingRealm {

    @Autowired
    @Lazy // 延迟加载
    private UserService userService;

}
总结:

这里给大家简单介绍引起事务不生效的几个原因

上一篇下一篇

猜你喜欢

热点阅读