描述:
我使用了@EnableAspectJAutoProxy(exposeProxy = true)
,但是我无法通过 获取当前的AOP代理AopContex.currentProxy()
。
环境: Spring Boot(2.1.4.RELEASE)。
该项目只有两个类:
@SpringBootApplication
@EnableAsync
@EnableAspectJAutoProxy(exposeProxy = true)
public class DemoTest2Application {
public static void main(String[] args) {
SpringApplication.run(DemoTest2Application.class, args);
}
}
@Controller
public class EmailController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
@ResponseBody
public Object asyncCall() throws InterruptedException {
System.out.println("before....");
((EmailController) AopContext.currentProxy()).testAsyncTask();
System.out.println("after....");
return "OK";
}
@Async
public void testAsyncTask() throws InterruptedException {
Thread.sleep(10000);
System.out.println("异步任务执行完成!");
}
}
当我请求“localhost:8080/test”时。我得到异常:
java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.
at org.springframework.aop.framework.AopContext.currentProxy(AopContext.java:69) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
翻遍各种文件,我也不知道自己哪里错了。