Spring AOP基础组件 PointcutSpring AOP基础组件 Advice 简介
持有AOP Advice和决定Advice适用性的过滤器(比如Pointcut)的基础接口;
Spring AOP基于符合AOP拦截API的MethodInterceptor支持通知,Advisor接口允许支持不同类型的通知,比如:前置通知、后置通知这些不需要通过拦截器实现的通知;
public interface Advisor { // 空通知 Advice EMPTY_ADVICE = new Advice() {}; // 获取通知,可以是拦截器、前置通知、抛出异常通知等 Advice getAdvice(); // 是否和每个被通知的实例相关 // 当前框架还未用到该方法,可以通过单例/多例,或者恰当的代理创建来确保Advisor的生命周期正确 boolean isPerInstance();}
实现子类 IntroductionAdvisor 简介支持AOP 引介功能的Advisor的基础接口;
核心代码public interface IntroductionAdvisor extends Advisor, IntroductionInfo { // 获取决定引介适用性的类过滤器 ClassFilter getClassFilter(); // 校验被通知的接口是否可以应用该IntroductionAdvisor void validateInterfaces() throws IllegalArgumentException;}
DefaultIntroductionAdvisor 简介IntroductionAdvisor的简单实现,默认地应用引介到任意类型;
核心代码public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFilter, Ordered, Serializable { private final Advice advice; private final Set
由Pointcut驱动的Advisor的基础接口;
含盖了除引介Advisor外几乎所有的Advisor;
public interface PointcutAdvisor extends Advisor { // 获取Pointcut Pointcut getPointcut();}
StaticMethodMatcherPointcutAdvisor 简介同时作为StaticMethodMatcherPointcut的Advisor的基础抽象类;
核心代码public abstract class StaticMethodMatcherPointcutAdvisor extends StaticMethodMatcherPointcut implements PointcutAdvisor, Ordered, Serializable { // 默认空通知 private Advice advice = EMPTY_ADVICE; public StaticMethodMatcherPointcutAdvisor(Advice advice) { // 校验Advice Assert.notNull(advice, "Advice must not be null"); this.advice = advice; } @Override public Advice getAdvice() { return this.advice; } @Override public boolean isPerInstance() { return true; } @Override public Pointcut getPointcut() { // 返回自身 return this; }}
AspectJPointcutAdvisor 简介适配AbstractAspectJAdvice为PointcutAdvisor接口;
核心代码public class AspectJPointcutAdvisor implements PointcutAdvisor, Ordered { private final AbstractAspectJAdvice advice; private final Pointcut pointcut; public AspectJPointcutAdvisor(AbstractAspectJAdvice advice) { // 校验Advice Assert.notNull(advice, "Advice must not be null"); this.advice = advice; this.pointcut = advice.buildSafePointcut(); } @Override public boolean isPerInstance() { return true; } @Override public Advice getAdvice() { return this.advice; } @Override public Pointcut getPointcut() { return this.pointcut; }}
AsyncAnnotationAdvisor 简介Spring Async功能的实现,支持通过@Async注解和javax.ejb.Asynchronous注解来激活异步方法执行的Advisor;
核心代码public class AsyncAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware { private Advice advice; private Pointcut pointcut; public AsyncAnnotationAdvisor( @Nullable Supplier
Spring Cache功能的实现,基于CacheOperationSource实现的Advisor,内部持有Advice实例;
核心代码public class BeanFactoryCacheOperationSourceAdvisor extends AbstractBeanFactoryPointcutAdvisor { // 判断当前方法是否有缓存注解,被CacheOperationSourcePointcut用来实现切入点逻辑 @Nullable private CacheOperationSource cacheOperationSource; // 使用基于CacheOperationSource的CacheOperationSourcePointcut作为pointcut private final CacheOperationSourcePointcut pointcut = new CacheOperationSourcePointcut() { @Override @Nullable protected CacheOperationSource getCacheOperationSource() { return cacheOperationSource; } }; @Override public Pointcut getPointcut() { return this.pointcut; }}
BeanFactoryTransactionAttributeSourceAdvisor 简介Spring 注解驱动的事务管理功能的实现,基于TransactionAttributeSource实现的Advisor,内部持有Advice实例;
核心方法public class BeanFactoryTransactionAttributeSourceAdvisor extends AbstractBeanFactoryPointcutAdvisor { // 判断当前方法是否有@Transactional注解,被TransactionAttributeSourcePointcut用来实现切入点逻辑 @Nullable private TransactionAttributeSource transactionAttributeSource; // 使用基于TransactionAttributeSource的TransactionAttributeSourcePointcut作为pointcut private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() { @Override @Nullable protected TransactionAttributeSource getTransactionAttributeSource() { return transactionAttributeSource; } }; @Override public Pointcut getPointcut() { return this.pointcut; }}
TransactionAttributeSourceAdvisor 简介Spring 注解驱动的事务管理功能的实现,基于TransactionAttributeSource实现的Advisor,内部持有TransactionInterceptor实例;
核心代码public class TransactionAttributeSourceAdvisor extends AbstractPointcutAdvisor { // 被TransactionAttributeSourcePointcut用来实现切入点逻辑 @Nullable private TransactionInterceptor transactionInterceptor; // 使用基于TransactionAttributeSource的TransactionAttributeSourcePointcut作为pointcut private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() { @Override @Nullable protected TransactionAttributeSource getTransactionAttributeSource() { return (transactionInterceptor != null ? transactionInterceptor.getTransactionAttributeSource() : null); } }; @Override public Advice getAdvice() { Assert.state(this.transactionInterceptor != null, "No TransactionInterceptor set"); return this.transactionInterceptor; } @Override public Pointcut getPointcut() { return this.pointcut; }}
DefaultPointcutAdvisor 简介几乎最通用的PointcutAdvisor的实现,可以使用任意类型Pointcut和除引介外任意类型Advice;
核心代码public class DefaultPointcutAdvisor extends AbstractGenericPointcutAdvisor implements Serializable { private Pointcut pointcut = Pointcut.TRUE; public DefaultPointcutAdvisor(Pointcut pointcut, Advice advice) { this.pointcut = pointcut; setAdvice(advice); } public void setPointcut(@Nullable Pointcut pointcut) { this.pointcut = (pointcut != null ? pointcut : Pointcut.TRUE); } @Override public Pointcut getPointcut() { return this.pointcut; }}
NameMatchMethodPointcutAdvisor 简介使用NameMatchMethodPointcut的Advisor;
核心代码public class NameMatchMethodPointcutAdvisor extends AbstractGenericPointcutAdvisor { private final NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut(); public NameMatchMethodPointcutAdvisor(Advice advice) { setAdvice(advice); } public void setMappedName(String mappedName) { this.pointcut.setMappedName(mappedName); } public void setMappedNames(String..、mappedNames) { this.pointcut.setMappedNames(mappedNames); } public NameMatchMethodPointcut addMethodName(String name) { return this.pointcut.addMethodName(name); } @Override public Pointcut getPointcut() { return this.pointcut; }}
RegexpMethodPointcutAdvisor 简介使用AbstractRegexpMethodPointcut的Advisor;
核心代码public class RegexpMethodPointcutAdvisor extends AbstractGenericPointcutAdvisor { @Nullable private String[] patterns; @Nullable private AbstractRegexpMethodPointcut pointcut; public RegexpMethodPointcutAdvisor(String pattern, Advice advice) { setPattern(pattern); setAdvice(advice); } public RegexpMethodPointcutAdvisor(String[] patterns, Advice advice) { setPatterns(patterns); setAdvice(advice); } @Override public Pointcut getPointcut() { synchronized (this.pointcutMonitor) { if (this.pointcut == null) { this.pointcut = createPointcut(); if (this.patterns != null) { //设置正则表达式 this.pointcut.setPatterns(this.patterns); } } return this.pointcut; } } protected AbstractRegexpMethodPointcut createPointcut() { // 使用JdkRegexpMethodPointcut return new JdkRegexpMethodPointcut(); }}