当前位置:网站首页>Cannot find current proxy: Set ‘exposeProxy‘ property on Advised to ‘true‘ to make it available
Cannot find current proxy: Set ‘exposeProxy‘ property on Advised to ‘true‘ to make it available
2022-07-26 18:09:00 【灼烧的疯狂】
前言
我这儿是在AOP中,通过 ProceedingJoinPoint.getTarget() 获取到 serviceImpl 的 object 实例,然后通过反射调用当前service的其他方法进行返回
报错是因为在反射调用的serviceImpl中的方法中,又去调用了当前serviceImpl中的 @Async 方法
报错正文:
Cannot find current proxy: Set ‘exposeProxy’ property on Advised to ‘true’ to make it available, and ensure that AopContext.currentProxy() is invoked in the same thread as the AOP invocation context.
解决方法
- 添加注解配置项
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
- exposeProxy = true
指定代理被AOP框架作为ThreadLocal暴露出来,可以被 AopContext 类获取。默认 false
- proxyTargetClass = true
指示是否要创建基于子类(CGLIB)的代理,而不是基于标准Java接口的代理。默认为false。
到这一步后,按网上说的,在被调用的 @Async 方法上再添加事务的注解 @Transactional,然后很遗憾,没什么卵用…
debug观察到,ProceedingJoinPoint.getTarget() 获取到的对象是具体的 serviceImpl实现类,不是cglib的代理类,于是找到了解题思路
- 将反射使用的 object 用Spring的 cglibObject 代替
修改代码,用 cglibTarget 代替 target 来做反射调用
// 这里的pjp 是 AOP 的 ProceedingJoinPoint
Object target = pjp.getTarget();
Class<?> targetClass = target.getClass();
// 使用spring的cglib代理,代替jdk的反射 - 这样在反射调用的方法里,才能使用 @Async 开启异步线程
Object cglibTarget = SpringUtil.getBean((targetClass.getInterfaces()[0]));
亲测OK,可以成功触发 @Async 的异步操作
希望对后面的兄弟们有所帮助或启发
边栏推荐
- MySQL - 多表查询与案例详解
- 2022年流动式起重机司机考试试题模拟考试平台操作
- 2022 mobile crane driver test questions simulation test platform operation
- Simulated 100 questions and simulated examination of refrigeration and air conditioning equipment operation examination in 2022
- rancher部署kubernetes集群
- VTK (the Visualization Toolkit) loads STL models
- CoVOS:无需解码!利用压缩视频比特流的运动矢量和残差进行半监督的VOS加速(CVPR 2022)...
- 【AUTOSAR-RTE】-1-聊一聊RTE(Run-Time Environment)
- Daorayaki | product principles of non-financial decentralized application
- 2022 tea master (intermediate) examination question simulation examination question bank and answers
猜你喜欢
随机推荐
Basic module and example pytorch learning
[AUTOSAR RTE] - 1-talk about RTE (run time environment)
MySQL - function and constraint commands
MySQL日志介绍
JS question brushing plan - linked list
ZbxTable 2.0 重磅发布!6大主要优化功能!
【Swoole系列3.1】进程、线程、协程,面试你被问了吗?
.net CLR GC dynamic loading transient heap threshold calculation and threshold excess calculation
Utility website recommendations
2022上海市安全员C证操作证考试题库模拟考试平台操作
Here comes the most complete introduction to MES system
2022g1 industrial boiler stoker certificate question bank and simulation examination
2022年焊工(初级)操作证考试题库及模拟考试
Verification palindrome string II of leetcode simple question
Last blog post
多线程学习笔记-1.CAS
Tensorflow-GPU 1.15安装
Leetcode simple question: the minimum total time required to fill a cup
Tensorflow GPU 1.15 installation
2022G1工业锅炉司炉上岗证题库及模拟考试









