当前位置:网站首页>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 - 函数及约束命令
- ReentrantLock学习之---基本属性
- Seata 入门简介
- rancher部署kubernetes集群
- VTK (the Visualization Toolkit) loads STL models
- TypeScript阶段学习
- LeetCode笔记:Weekly Contest 303
- Covos: no need to decode! Semi supervised Vos acceleration using motion vectors and residuals of compressed video bitstreams (CVPR 2022)
- Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release
- LeetCode简单题之验证回文字符串 Ⅱ
猜你喜欢

5款WPS Office最佳海外替代品

Comparison of advantages and disadvantages between SD NAND and EMMC

LeetCode简单题之验证回文字符串 Ⅱ
![[MySQL from introduction to proficiency] [advanced chapter] (VIII) clustered index & non clustered index & joint index](/img/18/1644301d575fad0a0d0f15932487d0.png)
[MySQL from introduction to proficiency] [advanced chapter] (VIII) clustered index & non clustered index & joint index

LeetCode-138-复制带随机指针的链表

MySQL learning notes -2. how to improve the query performance of SQL statements

篇7:exited on DESKTOP-DFF5KIK with error code -1073741511.

VPC nat (Sant, nant) experiment

Article 7:exited on desktop-dff5kik with error code -1073741511

Brian behrendorf, general manager of openssf Foundation: it is estimated that there will be 420million open sources in 2026
随机推荐
Brian behrendorf, general manager of openssf Foundation: it is estimated that there will be 420million open sources in 2026
[yuntu said] issue 246 digital asset chain - your God of digital asset property protection!
MongoDB stats统计集合占用空间大小
2022年化工自动化控制仪表考题模拟考试平台操作
简述MES系统的11大核心功能模块
Daorayaki | product principles of non-financial decentralized application
Introduction to Seata
Briefly describe the 11 core functional modules of MES system
Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release
Racher deploys kubernetes cluster
ReentrantLock学习之公平锁过程
LeetCode笔记:Biweekly Contest 83
自动化测试的使用场景
Leetcode notes: Weekly contest 303
[postgraduate entrance examination vocabulary training camp] day 13 - reliance, expert, subject, unconscious, photograph, exaggeration, counter act
CoVOS:无需解码!利用压缩视频比特流的运动矢量和残差进行半监督的VOS加速(CVPR 2022)...
MES系统的选择需重点考虑哪些方面?
CTO will teach you: how to take over his project when a technician suddenly leaves
Seata 入门简介
MySQL log introduction