這篇文章主要介紹“Springframework中的ReflectiveAspectJAdvisorFactory有什么作用”,在日常操作中,相信很多人在Springframework中的ReflectiveAspectJAdvisorFactory有什么作用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Springframework中的ReflectiveAspectJAdvisorFactory有什么作用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)是一家成都網(wǎng)站設(shè)計、做網(wǎng)站,提供網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,網(wǎng)站制作,建網(wǎng)站,按需網(wǎng)站建設(shè),網(wǎng)站開發(fā)公司,從2013年開始是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價值為核心業(yè)務(wù),全程參與項目的網(wǎng)站策劃設(shè)計制作,前端開發(fā),后臺程序制作以及后期項目運營并提出專業(yè)建議和思路。
Spring版本是5.0.4.release.
ReflectiveAspectJAdvisorFactory這個類,個人覺得是Spring aop的一個核心類。如下List-1所示,ReflectiveAspectJAdvisorFactory間接實現(xiàn)了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方法會根據(jù)方法上的注解,創(chuàng)建對應(yīng)的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); } ...
到此,關(guān)于“Springframework中的ReflectiveAspectJAdvisorFactory有什么作用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當(dāng)前名稱:Springframework中的ReflectiveAspectJAdvisorFactory有什么作用
URL鏈接:http://weahome.cn/article/ijpecc.html