前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Springframework之ReflectiveAspectJAdvisorFactory

Springframework之ReflectiveAspectJAdvisorFactory

作者头像
克虏伯
发布2019-07-15 17:06:59
4320
发布2019-07-15 17:06:59
举报

Spring版本是5.0.4.release.

ReflectiveAspectJAdvisorFactory这个类,个人觉得是Spring aop的一个核心类。如下List-1所示,ReflectiveAspectJAdvisorFactory间接实现了AspectJAdvisorFactory。

List-1

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
@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

代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reference
相关产品与服务
云顾问
云顾问(Tencent Cloud Smart Advisor)是一款提供可视化云架构IDE和多个ITOM领域垂直应用的云上治理平台,以“一个平台,多个应用”为产品理念,依托腾讯云海量运维专家经验,助您打造卓越架构,实现便捷、灵活的一站式云上治理。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档