- 1. 概要
- 2. 工具封装的方法
- 3. 目前SpringBoot的应用启动类如下:
- 4. 解决
- 4.1 分析
- 4.2完整启动类配置
最近在项目中封装工具类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(2. 工具封装的方法) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
@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)
@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);
}
}



