1吊圾、EnableMyAsync里面有@Import(MyAsyncConfigurationSelector.class)
2、MyAsyncConfigurationSelector根據(jù)條件選擇代理配置呈枉,ProxyAsyncConfiguration為其中一種
生成AsyncAnnotationBeanPostProcessor 對(duì)象趁尼,并放入容器
3-1、ProxyAsyncConfiguration有@Bean注解的方法- AsyncAnnotationBeanPostProcessor asyncAdvisor()猖辫,生成AsyncAnnotationBeanPostProcessor 對(duì)象
3-2酥泞、ProxyAsyncConfiguration的父類AbstractAsyncConfiguration,方法setConfigurers中注入configurers對(duì)象啃憎,從中獲取executor對(duì)象
3-3芝囤、AsyncDefaultAutoConfiguration對(duì)象是一個(gè)條件配置類,@ConditionalOnProperty(value = "spring.sleuth.async.enabled", matchIfMissing = true)說(shuō)明默認(rèn)是生效的辛萍,會(huì)提供一個(gè)實(shí)現(xiàn)了AsyncConfigurer接口的
DefaultAsyncConfigurerSupport對(duì)象悯姊,接口方法getAsyncExecutor返回的是new SimpleAsyncTaskExecutor()的代理類LazyTraceExecutor
接3-1
把Async注解與異步執(zhí)行串起來(lái):
4-1、AsyncAnnotationBeanPostProcessor里有setBeanFactory方法贩毕,里面指定了AsyncAnnotationAdvisor對(duì)象為注解增強(qiáng)器對(duì)象
4-2悯许、AsyncAnnotationAdvisor的構(gòu)造方法中,指定了Async注解為想要增強(qiáng)的注解辉阶,并調(diào)用buildPointcut方法生成與注解匹配的Pointcut對(duì)象先壕;同時(shí)會(huì)調(diào)用buildAdvice生成Advice對(duì)象
4-3瘩扼、buildAdvice生成的Advice對(duì)象是AnnotationAsyncExecutionInterceptor實(shí)例,其父類為AsyncExecutionInterceptor垃僚,實(shí)現(xiàn)了接口MethodInterceptor的invoke方法集绰,會(huì)把原來(lái)的代碼封裝成一個(gè)任務(wù)交給executor對(duì)象來(lái)異步執(zhí)行
把程序啟動(dòng)與bean生成代理類對(duì)象串起來(lái):
5-1、AsyncAnnotationBeanPostProcessor父類的父類- AbstractAdvisingBeanPostProcessor谆棺,實(shí)現(xiàn)了BeanPostProcessor接口的postProcessAfterInitialization栽燕,在對(duì)象實(shí)例化之后的處理,如果對(duì)象的類實(shí)現(xiàn)了Advised接口或者對(duì)象的方法有需要增強(qiáng)包券,則會(huì)生成一個(gè)代理類對(duì)象返回纫谅,否則返回原對(duì)象