溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Springframework中的ReflectiveAspectJAdvisorFactory有什么作用

發(fā)布時(shí)間:2021-11-16 15:03:32 來(lái)源:億速云 閱讀:150 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“Springframework中的ReflectiveAspectJAdvisorFactory有什么作用”,在日常操作中,相信很多人在Springframework中的ReflectiveAspectJAdvisorFactory有什么作用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Springframework中的ReflectiveAspectJAdvisorFactory有什么作用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

    Spring版本是5.0.4.release.

    ReflectiveAspectJAdvisorFactory這個(gè)類(lèi),個(gè)人覺(jué)得是Spring aop的一個(gè)核心類(lèi)。如下List-1所示,ReflectiveAspectJAdvisorFactory間接實(shí)現(xiàn)了AspectJAdvisorFactory。

    List-1

public interface AspectJAdvisorFactory {
  
	boolean isAspect(Class<?> clazz);

	void validate(Class<?> aspectClass) throws AopConfigException;

	List<Advisor> getAdvisors(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方法判斷類(lèi)上是否有Aspect注解,BeanFactoryAspectJAdvisorsBuilder就用這個(gè)方法判斷一個(gè)類(lèi)上是有Aspect注解。

    有必要來(lái)看下AbstractAspectJAdvisorFactory這個(gè)類(lèi),因?yàn)镽eflectiveAspectJAdvisorFactory繼承了它。如下List-2所示,isAspect方法判斷類(lèi)上是有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所示,這個(gè)類(lèi)還有個(gè)工具方法,查看方法上面是否有Pointcut.class, Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class注解,如果找到一個(gè),則封裝為AspectJAnnotation返回。 

  List-3

@Nullable
protected static AspectJAnnotation<?> findAspectJAnnotationOnMethod(Method method) {
  for (Class<?> clazz : ASPECTJ_ANNOTATION_CLASSES) {
    AspectJAnnotation<?> foundAnnotation = findAnnotation(method, (Class<Annotation>) clazz);
    if (foundAnnotation != null) {
      return foundAnnotation;
    }
  }
  return null;
}

@Nullable
private static <A extends Annotation> AspectJAnnotation<A> findAnnotation(Method method, Class<A> toLookFor) {
  A result = AnnotationUtils.findAnnotation(method, toLookFor);
  if (result != null) {
    return new AspectJAnnotation<>(result);
  }
  else {
    return null;
  }
}

    ReflectiveAspectJAdvisorFactory的getAdvice方法會(huì)根據(jù)方法上的注解,創(chuàng)建對(duì)應(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é)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI