Springframework之ReflectiveAspectJAdvisorFactory

本文深入探讨了Spring框架中AOP模块的核心组件ReflectiveAspectJAdvisorFactory,详细解释了其如何实现AspectJAdvisorFactory接口,以及在判断Aspect注解、创建Advice等方面的具体作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

    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;

	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方法判断类上是否有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<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方法会根据方法上的注解,创建对应的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);
  }
...

Reference

  1. https://github.com/spring-projects/spring-framework/tree/5.0.x

转载于:https://my.oschina.net/u/2518341/blog/3073109

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值