描述

这个BUG大的起源是我上线以后,在后台看日志的时候发现一行奇怪的INFO日志:

2022-06-09 23:34:24 [restartedMain] [org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker:376] INFO  - Bean 'userServiceImpl' of type [com.markerhub.service.impl.UserServiceImpl] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

什么?userServiceImpl代理失效了?我于是尝试了一下,果然,这个方法直接不回滚了:

@Override
@Transactional
public void addUser(UserVo user) {
User userExist = getBaseMapper().selectOne(new QueryWrapper<User>().eq("id", user.getId())); if (userExist == null) {//添加
user.setPassword(SecureUtil.md5(user.getPassword()));
user.setCreated(LocalDateTime.now());
user.setLastLogin(LocalDateTime.now());
boolean update = saveOrUpdate(user);
log.info("添加{}号账号结果:{}", user.getId(), update);
int i = 1 / 0;
Assert.isTrue(update, "添加失败");
} else {//修改 BeanUtil.copyProperties(user, userExist, "created", "password", "lastLogin", "username", "id"); boolean update = saveOrUpdate(userExist); log.info("修改{}号账号结果:{}", userExist.getId(), update); Assert.isTrue(update, "修改失败");
} }

第12行我强行模拟了故障,但是数据库里还是保存成功了。这个是生产上的严重事故。Spring事务默认的传播级别,是当前若存在事务,就加入这个事务。saveOrUpdate是mybatis-plus的方法,已经加上了事务注解,也就是说这里事务本来必须是其作用的。

原理探究

我从网上的一篇文章发现了答案https://blog.csdn.net/feiying0canglang/article/details/119704109,问题的罪魁祸首是Shiro框架。

在使用shiro框架的时候,一般都要新建一个ShiroFilterFactoryBean配置一些配置,例如:

@Bean("shiroFilterFactoryBean")
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager,
ShiroFilterChainDefinition shiroFilterChainDefinition) {
ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
shiroFilter.setSecurityManager(securityManager);
Map<String, Filter> filters = new HashMap<>();
filters.put("jwt", jwtFilter);
shiroFilter.setFilters(filters);
Map<String, String> filterMap = shiroFilterChainDefinition.getFilterChainMap();
shiroFilter.setFilterChainDefinitionMap(filterMap);
return shiroFilter;
}

这些本身是一些固定的写法,没有太多可以改的,抄过来就行了。但是问题在于ShiroFilterFactoryBean实现了BeanPostProcessor这个接口:

public class ShiroFilterFactoryBean implements FactoryBean<AbstractShiroFilter>, BeanPostProcessor;

这个接口的实现类会在某种情况下被提前初始化,导致他不会被Sping AOP的动态代理所处理,最后的结果就是事务失效。

从打印日志看到,Realm是没有被代理的,因为Realm依赖了UserService,最后UserService页没有被代理。UserService也可能依赖了其他Service,最后就全都事务失效了。

解决

在UserService上加入@Lazy懒加载模式:

UserService userService;

    @Autowired
@Lazy
private void setUserServiceImpl(UserService userService) {
this.userService = userService;
}

分析依赖,将提前加载的Service全部懒加载。

not eligible for getting processed by all BeanPostProcessors的更多相关文章

  1. is not eligible for getting processed by all BeanPostProcessors

    BeanPostProcessor是控制Bean初始化开始和初始化结束的接口.换句话说实现BeanPostProcessor的bean会在其他bean初始化之前完成,BeanPostProcessor ...

  2. is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

    出现此日志的原因: https://blog.csdn.net/m0_37962779/article/details/78605478 上面的博客中可能解决了他的问题,可我的项目是spring bo ...

  3. BeanPostProcessors (for example: not eligible for auto-proxying),报错解决

    最近遇到个问题,springmvc项目debug启动失败 debug启动会卡住不动,run模式启动正常 debug启动输出到下面这行之后,就不会继续输出了 -- :: [INFO]- Bean 'da ...

  4. idea-debug启动会卡住不动,BeanPostProcessors (for example: not eligible for auto-proxying),报错解决

    debug启动会卡住不动,run模式启动正常 debug启动输出到下面这行之后,就不会继续输出了 爆出各种 [INFO]- Bean 'dataSource' of type [class Druid ...

  5. spring boot 代理(not eligible for auto-proxying)

    spring 事务机制网上的案例很多,关于事务 不能回滚也有很多的类型,不同的问题有不同的处理方案,本篇博客主要介绍两种事务不能回滚的问题解决方案: 问题一:    在同一个对象中有两个方法,分别未方 ...

  6. Dubbo消费端错误: ClassNotFoundException: org.apache.zookeeper.proto.WatcherEvent

    出现错误的原因是消费端war没有启动成功, 但是zkClient和Dubbo的对应Thread启动了, web container无法加载对应的类, INFO: Initializing Protoc ...

  7. Spring Autowired错误???

    @SpringBootApplicationpublic class TestMqApplication extends SpringBootServletInitializer { @Suppres ...

  8. spring-boot支持双数据源mysql+mongo

    这里,首先想说的是,现在的web应用,处理的数据对象,有结构化的,也有非结构化的.同时存在.但是在spring-boot操作数据库的时候,若是在properties文件中配置数据源的信息,通过默认配置 ...

  9. Spring boot配合Spring session(redis)遇到的错误

    背景:本MUEAS项目,一开始的时候,是没有引入redis的,考虑到后期性能的问题而引入.之前没有引用redis的时候,用户登录是正常的.但是,在加入redis支持后,登录就出错!错误如下: . __ ...

  10. Logback相关知识汇总

    例如:%-4relative 表示,将输出从程序启动到创建日志记录的时间 进行左对齐 且最小宽度为4格式修饰符,与转换符共同使用:可选的格式修饰符位于“%”和转换符之间.第一个可选修饰符是左对齐 标志 ...

随机推荐

  1. [Swift]使用Alamofire传递参数时报错

    p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1) } span.s1 { font-variant-ligatures: no-c ...

  2. SQL Server 2012主从数据库的订阅和发布,实现数据库读写分离(主从备份)

    学习:https://www.bilibili.com/video/BV13B4y1h7Wu?p=12&spm_id_from=pageDriver&vd_source=3f21d2e ...

  3. Python学习笔记(二)变量的使用

    一.变量的定义 把程序运算的中间结果临时存到内存里,以备后面的代码继续调用,这几个名字的学名就叫做"变量" 可以把变量看做保存信息的容器,它们的目的是在内存中标注和存储数据,然后可 ...

  4. 使用layui实现分页展示数据库的数据

    layui是一个前端 UI 框架,内置了js代码,所以我们可以直接使用内置的分页 首先要用到layui的官网手册https://www.layui.com/ 1.进入手册页面的 "示例&qu ...

  5. (0617 ) centos7运行脚本提示: 没有那个文件或目录 :No such file or directory

    https://blog.csdn.net/hehuihh/article/details/88174007 之前也 遇到: https://www.cnblogs.com/fancy2333/p/1 ...

  6. 使用 FreeSSL 申请免费证书

    官网 https://freessl.cn/ 首先,注册一个账户 然后登录 输入自己的域名,选择第2个"亚洲诚信"(1年),然后点击"创建免费SSL证书"按钮 ...

  7. 为什么要有jvm,jvm的作用?

    jvm的两个作用:第一.运行并管理java源码文件所生成的Class文件.第二.在不同的操作系统上安装不同的jvm,从而去实现跨平台的一个保障. 一般情况下,即使不熟悉jvm的运行机制,也不影响业务代 ...

  8. Java 接口与接口的多继承关系

    接口与接口之间是多继承的 注意事项:1. 多个父接口中的抽象方法重复,没关系2. 多个父接口中默认方法重复,子接口必须进行默认方法的覆盖重写 //接口A public interface MyInte ...

  9. pyintaller 打包后报No module named 'XXX'

    在pycharm中运行一切正常,但是使用pyinstaller打包之后,双击exe就提示缺乏某某module 百度一番之后,尝试了说hidden-import之类的,以及说只留一个主程序在最外层啥的, ...

  10. dynamics 365/crm 导入解决方案报 发生 sql server 错误

    dynamics 365/crm 导入解决方案报 发生 sql server 错误.{1}{0} 错误代码 80044150. 帮助我解决此问题. 这时候,可以检查数据库服务器的日志看看,可能会找到S ...