SpringBoot项目中的用法:
@EnableAsync
在哪个模块使用异步那么启动类必须要加上。
@Async
一般是加载没有实现接口得到方法上,为什么呢?因为默认使用的是CGLIB动态代理,而实现了接口又重写了接口中对应的方法,这个异步的代理就会走JDK动态代理,此时会报错如下:
@Async
@Override(此时实现了接口中的方法又使用了异步,那么如果不强制那么就会报错。)
public Future<R<Boolean>> getByUserId(String userId):
> Action:
>
> Consider injecting the bean as one of its interfaces or forcing the
> use of CGLib-based proxies by setting proxyTargetClass=true on
> @EnableAsync and/or @EnableCaching.
解决方案:
在父注解上加上强制使用CGLIB动态代理的配置:@EnableAsync(proxyTargetClass = true)