1.在启动类,加入@EnableAsync 注解
@SpringBootApplication @EnableAsync public class AsyncLearnApplication { public static void main(String[] args) { SpringApplication.run(AsyncLearnApplication.class, args); } }
2.在需要使用异步的方法添加 @Async 注解
@Service public class AsyncService { private final Logger logger = LoggerFactory.getLogger(AsyncService.class); @Async("asyncExecutor") public void importOrderInfo(MultipartFile file) throws Exception { logger.info("数据处理中..."); ImportParams params = new ImportParams(); params.setHeadRows(1); try { List<OrderExcelDTO> list = ExcelImportUtil.importExcel(file.getInputStream(), OrderExcelDTO.class, params); logger.info("list size:[{}]",list.size()); logger.info("数据处理完成"); }catch (Exception e){ logger.error("文件转换异常[{}]",e); } } }