EnableAsync
使用注解 @EnableAsync 开启异步,会自动扫描
@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.cdw.springbootmavemdemo.mapper")//导入 org.mybatis.spring.annotation.MapperScan;就会报错
//扫描 所有需要的包, 包含一些自用的工具类包 所在的路径
@ComponentScan(basePackages= {
"com.cdw.springbootmavemdemo", "org.n3r.idworker"})
//开启定时任务
@EnableScheduling
//开启异步调用方法
@EnableAsync
public class SpringBootMavemDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMavemDemoApplication.class, args);
}
}
Component 和 Async
定义 @Component 和 @Async作为组件被容器扫描执行
AsyncTask .java
package com.cdw.springbootmavemdemo.tasks;
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.