@EnableAsync
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
Class<? extends Annotation> annotation() default Annotation.class;
boolean proxyTargetClass() default false;
AdviceMode mode() default AdviceMode.PROXY;
int order() default Ordered.LOWEST_PRECEDENCE;
}
@EnableAsync 注解用于开启 Spring 对方法异步执行的能力,需要和注解@Configuration 配合使用。
@Configuration
@EnableAsync
public class AppConfig {
}
也可以自定义线程池:
@Configuration
@EnableAsync
public class AppConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize();
executor.setQueueCapacity();
executor.setThreadNamePrefix();
executor.initialize();
executor;
}
AsyncUncaughtExceptionHandler {
();
}
}


