本篇文章主要介绍了Spring @Async 的使用与实现的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
首先Spring AOP有两个重要的基础接口,Advisor和PointcutAdvisor,接口声明如下:
Advisor接口声明:
public interface Advisor { Advice getAdvice(); boolean isPerInstance(); }
PointcutAdvisor的接口声明:
public interface PointcutAdvisor extends Advisor { /** * Get the Pointcut that drives this advisor. */ Pointcut getPointcut(); }
PointcutAdvisor用来获取一个切点以及这个切点的处理器(Advise)。
@Async注解使用后置处理器BeanPostProcessor的子类AsyncAnnotationBeanPostProcessor来实现bean处理 :
AsyncAnnotationAdvisor继承了PointcutAdvisor接口。并且在AsyncAnnotationBeanPostProcessor实现了其父类接口的BeanFactoryAware中的setBeanFactory初始化。Spring一旦创建beanFactory回调成功,就会回调这个方法。保证Advisor对象最先被初始化。
@Override public void setBeanFactory(BeanFactory beanFactory) { super.setBeanFactory(beanFactory); AsyncAnnotationAdvisor advisor = new AsyncAnnotationAdvisor(this.executor, this.exceptionHandler); if (this.asyncAnnotationType != null) { advisor.setAsyncAnnotationType(this.asyncAnnotationType); } advisor.setBeanFactory(