您现在的位置是:首页 > 技术人生 > 后端技术后端技术

记一次SpringBoot Aspect不生效解决过程

高晓波2020-10-22【后端技术】人已围观

简介项目中两个aspect,一个环绕controller,用于记录日志,能够正常在point处进入aspect处理;另一个aspect 的point设于service,死活不能进入。

问题描述

项目中两个aspect,一个环绕controller,用于记录日志,能够正常在point处进入aspect处理;另一个aspect 的point设于service,死活不能进入。


解决思路

1、首先排查pointcut配置是否正确,检查后发现没有问题;
2、我们都知道spring的aop运用的是动态代理技术,由spring托管的bean大多为代理bean,controller层打印service对象,发现service对象竟然直接是service实现类的“本尊”。如下图所示:

debug

再看springboot启动日志,发现roleServiceImpl实例化的时候有如下提示:
Bean 'roleServiceImpl' of type [com.gaoxiaobo.wms.service.impl.RoleServiceImpl] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

debug

那么问题就明了了,我们在controller层调用的roleServiceImpl对象非代理对象而是其本身,那么对其的aspect是不会生效的,不仅aspsect不会生效,事务注解@Transactional也不会生效,问题还是相当严重的。

那么,why 没有生成其代理对象呢?一定是哪里先调用了
roleServiceImpl导致spring优先实例化了该bean;

通过排查,找到了罪魁祸首:shiro 的LifecycleBeanPostProcessor优先实例化自定义Realm,自定义的Realm依赖于roleService,导致roleService被初始化。



解决方法
自定义realm中roleService设置成懒加载。



 

Tags:

很赞哦! ()

文章评论