springboot AOP对于相同切点,相同的方法去执行,切点的优先级以什么为准

提问:有4个相同的切点,切一个方法。然后还有4个相同的方法分别执行不同的方法,那么请问,对于这4个相同的切点,哪个优先级高,以什么为标准?

为什么要提出这个问题:完全是不小心想到这个问题

最终结果:最终没有找到源码依据,完全是靠统计学中的规律学获得最终结果(博客最后给答案),可行性78%(瞎说的)

 

相同的数据:相同的类,相同的切点,相同的执行方法,相同的提示语句

不同的数据:声明顺序不同,方法名不同,切点的方法名不同。

猜想:应该和顺序,方法名和切点的方法名有关。

。。。。。

省略中间过程,因为我感觉我要是看这个题目的就只想知道结果,所以我粘贴一个AOP类,有心的同学自己玩,这是springboot的Demo,我给结果算了,哈哈


import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AopTest3 {

    private static final Logger logger = Logger.getLogger(AopTest3.class);   //日志,我用的log4j


    //配置切点
    @Pointcut("execution(* com.sso.controller..*(..))")
    public void log1() {
    }

  @Pointcut("execution(* com.sso.controller..*(..))")
  public void log2() {
  }

  //配置切点
  @Pointcut("execution(* com.sso.controller..*(..))")
  public void log3() {
  }

  @Pointcut("execution(* com.sso.controller..*(..))")
  public void log4() {
  }

  @Before("log1()")  //前置通知
  public void a(JoinPoint joinPoint) {
    System.out.println("a".hashCode());
    logger.info("log1 先");
  }

    @Before("log2()")  //前置通知
    public void b(JoinPoint joinPoint) {
      System.out.println("b".hashCode());
      logger.info("log2 先");
    }

  @Before("log3()")  //前置通知
  public void d(JoinPoint joinPoint) {
    System.out.println("d".hashCode());
    logger.info("log3 先");
  }

  @Before("log4()")  //前置通知
  public void c(JoinPoint joinPoint) {
    System.out.println("c".hashCode());
    logger.info("log4 先");
  }


}

测试结果:相同的切点,执行的优先级和执行方法的方法名有关系,和方法名的hashCode有关,hashCode越小优先级越高。

注意:这个规律不是特别准,只有方法名简单的时候能看出这个规律,方法名太长的时候就看不出来了。。。。但是总归来说跟方法名肯定有关系,但是不知道这是不是唯一的标准。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章