Springframework中的ReflectiveAspectJAdvisorFactory有什么作用
这篇文章主要介绍“Springframework中的ReflectiveAspectJAdvisorFactory有什么作用”,在日常操作中,相信很多人在Springframework中的ReflectiveAspectJAdvisorFactory有什么作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Springframework中的ReflectiveAspectJAdvisorFactory有什么作用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联是一家成都网站设计、做网站,提供网页设计,网站设计,网站制作,建网站,按需网站建设,网站开发公司,从2013年开始是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。
Spring版本是5.0.4.release.
ReflectiveAspectJAdvisorFactory这个类,个人觉得是Spring aop的一个核心类。如下List-1所示,ReflectiveAspectJAdvisorFactory间接实现了AspectJAdvisorFactory。
List-1
public interface AspectJAdvisorFactory { boolean isAspect(Class> clazz); void validate(Class> aspectClass) throws AopConfigException; ListgetAdvisors(MetadataAwareAspectInstanceFactory aspectInstanceFactory); Advisor getAdvisor(Method candidateAdviceMethod, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName); Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName); }
List-1中的isAspect方法判断类上是否有Aspect注解,BeanFactoryAspectJAdvisorsBuilder就用这个方法判断一个类上是有Aspect注解。
有必要来看下AbstractAspectJAdvisorFactory这个类,因为ReflectiveAspectJAdvisorFactory继承了它。如下List-2所示,isAspect方法判断类上是有Aspect注解。
List-2
public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFactory { private static final String AJC_MAGIC = "ajc$"; private static final Class>[] ASPECTJ_ANNOTATION_CLASSES = new Class>[] { Pointcut.class, Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class}; protected final Log logger = LogFactory.getLog(getClass()); protected final ParameterNameDiscoverer parameterNameDiscoverer = new AspectJAnnotationParameterNameDiscoverer(); @Override public boolean isAspect(Class> clazz) { return (hasAspectAnnotation(clazz) && !compiledByAjc(clazz)); } private boolean hasAspectAnnotation(Class> clazz) { return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null); } ...
如下List-3所示,这个类还有个工具方法,查看方法上面是否有Pointcut.class, Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class注解,如果找到一个,则封装为AspectJAnnotation返回。
List-3
@Nullable protected static AspectJAnnotation> findAspectJAnnotationOnMethod(Method method) { for (Class> clazz : ASPECTJ_ANNOTATION_CLASSES) { AspectJAnnotation> foundAnnotation = findAnnotation(method, (Class) clazz); if (foundAnnotation != null) { return foundAnnotation; } } return null; } @Nullable private static AspectJAnnotation findAnnotation(Method method, Class toLookFor) { A result = AnnotationUtils.findAnnotation(method, toLookFor); if (result != null) { return new AspectJAnnotation<>(result); } else { return null; } }
ReflectiveAspectJAdvisorFactory的getAdvice方法会根据方法上的注解,创建对应的Advice,如下List-4所示
List-4
public Advice getAdvice(Method candidateAdviceMethod, AspectJExpressionPointcut expressionPointcut, MetadataAwareAspectInstanceFactory aspectInstanceFactory, int declarationOrder, String aspectName) { AbstractAspectJAdvice springAdvice; ... switch (aspectJAnnotation.getAnnotationType()) { case AtPointcut: if (logger.isDebugEnabled()) { logger.debug("Processing pointcut '" + candidateAdviceMethod.getName() + "'"); } return null; case AtAround: springAdvice = new AspectJAroundAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); break; case AtBefore: springAdvice = new AspectJMethodBeforeAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); break; case AtAfter: springAdvice = new AspectJAfterAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); break; case AtAfterReturning: springAdvice = new AspectJAfterReturningAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation(); if (StringUtils.hasText(afterReturningAnnotation.returning())) { springAdvice.setReturningName(afterReturningAnnotation.returning()); } break; case AtAfterThrowing: springAdvice = new AspectJAfterThrowingAdvice( candidateAdviceMethod, expressionPointcut, aspectInstanceFactory); AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation(); if (StringUtils.hasText(afterThrowingAnnotation.throwing())) { springAdvice.setThrowingName(afterThrowingAnnotation.throwing()); } break; default: throw new UnsupportedOperationException( "Unsupported advice type on method: " + candidateAdviceMethod); } ...
到此,关于“Springframework中的ReflectiveAspectJAdvisorFactory有什么作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
网站标题:Springframework中的ReflectiveAspectJAdvisorFactory有什么作用
标题链接:http://scyanting.com/article/ijpecc.html