栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

封装AopContext.currentProxy()对象失败

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

封装AopContext.currentProxy()对象失败

文章目录
  • 1. 概要
  • 2. 工具封装的方法
  • 3. 目前SpringBoot的应用启动类如下:
  • 4. 解决
    • 4.1 分析
    • 4.2完整启动类配置

1. 概要

最近在项目中封装工具类SpringUtils,其中有一个静态方法是获取aop代理对象,但是使用的时候确产生了如下的异常:

java.lang.IllegalStateException: 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.
	at org.springframework.aop.framework.AopContext.currentProxy(AopContext.java:69)
	at com.jlkj.jwxt.common.core.util.SpringUtils.getAopProxy(SpringUtils.java:98)
	at com.jlkj.jwxt.admin.service.impl.SysRoleServiceImpl.selectRoleAll(SysRoleServiceImpl.java:99)
	at com.jlkj.jwxt.admin.service.impl.SysRoleServiceImpl$$FastClassBySpringCGLIB$$b8930bc3.invoke()
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
2. 工具封装的方法
@Component
public final class SpringUtils implements BeanFactoryPostProcessor {


    @SuppressWarnings("unchecked")
    public static  T getAopProxy(T invoker) {
        return (T) AopContext.currentProxy();
    }
}
  
3. 目前SpringBoot的应用启动类如下:
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
@EnableAsync
@MapperScan("com.lfc.admin.mapper.**")
public class AdminSystemApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminSystemApplication.class, args);
    }
}
4. 解决 4.1 分析

通过报错信息可以看出缺少了属性 exposeProxy ,那么查找AOP相关的文档,或者开启的方式是在启动类上增加相应注解即可:
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)

4.2完整启动类配置
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
@EnableAsync
@MapperScan("com.lfc.admin.mapper.**")
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class AdminSystemApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminSystemApplication.class, args);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1020632.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号