文章目录
遇到一个问题,接口处理的速度很慢,导致前端一直在刷新中,于是想在后台开启异步处理。
因为是spring框架,但是直接使用@Async注解,项目启动会报获取bean失败的错误。
解决办法:
- 在启动类上加注解:@EnableAsync
- service层:
public interface AsyncService{
void asyncInvoke(AsyncExec consumer) throws Exception;
@FunctionalInterface
interface AsyncExec{
void exec() throws Exception;
}
}
- impl层
@Service
public class AsyncServiceImpl implements AsyncService{
@Async
@override
public void asyncInvoke(AsyncExec consumer)throws Exception{
consumer.exec()
}
}
- 用法
@Autowired
private AsyncService asyncService;
try{
asyncService.asyncInvoke(()->{
//需要异步的方法
})
}catch(Exception e){
}
。。。。


2923

被折叠的 条评论
为什么被折叠?



