springboot中的async注解表示异步执行,将其标注的方法交给线程池进行处理。
如果要在项目里使用async注解,需要在首先加上EnableAsync注解。
当然我们也可以使用“javax.ejb.Asynchronous”注解或者自定义注解实现异步,如果是自定义注解,需要在EnableAsync的annotation属性处指定。一旦指定了EnableAsync的annotation属性,则async和“javax.ejb.Asynchronous”注解将无效。
对于EnableAsync注解,通过@import引入了AsyncConfigurationSelector类。
public class AsyncConfigurationSelector extends AdviceModeImportSelector<EnableAsync> {
private static final String ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME =
"org.springframework.scheduling.aspectj.AspectJAsyncConfiguration";
/**
* {@inheritDoc}
* @return {@link ProxyAsyncConfiguration} or {@code AspectJAsyncConfiguration} for
* {@code PROXY} and {@code ASPECTJ} values of {@link EnableAsync#mode()}, respectively
*/
@Override
@Nullable
public String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:
return new String[] { ProxyAsyncConfiguration.class.getName() };
case ASPECTJ:
return new String[] { ASYNC_EXECUTION_ASPECT_CONFIGURATION_CLASS_NAME };
default:
return null;
}
}
}
springboot默认的adviceMode是proxy,将配置类ProxyAsyncConfiguration注入IOC容器。
在ProxyAsyncConfiguration分类AbstractAsyncConfiguration我们通过了与其它springboot中注解类似的方式实现类可配置化。
@Autowired(required = false)
void setConfigurers(Collection<AsyncConfigurer> configurers) {
if (CollectionUtils.isEmpty(configurers)) {
return;
}
if (configurers.size() > 1) {
throw new IllegalStateException("Only one AsyncConfigurer may exist");
}
AsyncConfigurer configurer = configurers.iterator().next();
this.executor = configurer.getAsyncExecutor();
this.exceptionHandler = configurer.getAsyncUncaughtExceptionHandler();
}
我们可以自定义一个实现AsyncConfigurer接口的类,在其中指定executor和exceptionHandler,并将其注入IOC容器来实现默认线程池和异常处理的定制化。
回到ProxyAsyncConfiguration类,这是一个configuration类,在其中通过@bean注入了AsyncAnnotationBeanPostProcessor 类。
@Bean(name = TaskManagementConfigUtils.ASYNC_ANNOTATION_PROCESSOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public AsyncAnnotationBeanPostProcessor asyncAdvisor() {
Assert.notNull(this.enableAsync, "@EnableAsync annotation metadata was not injected");
AsyncAnnotationBeanPostProcessor bpp = new AsyncAnnotationBeanPostProcessor();
Class<? extends Annotation> customAsyncAnnotation = this.enableAsync.getClass("annotation");
if (customAsyncAnnotation != AnnotationUtils.getDefaultValue(EnableAsync.class, "annotation")) {
bpp.setAsyncAnnotationType(customAs