spring aop 切点表达式 && advisor 执行顺序

390 阅读1分钟

pointcut 中@within 注解

target注解会在当前配置的包路径下的类中生效,而其子类不会生效,within 其子类继承父类得方法也生效,但是子类自己的方法和重写父类的方法都不会生效 不同表达式可以使用在一起混合使用


@Pointcut("within(com.example.aop.controller..*)&&@annotation(com.example.aop.annotation.Test)")
public void pointCut(){

}

//表示 切点方法必须要使用Test注解和必须在controller包下,对应的advisor才生效。同理,如果两个表达式使用||连接则表示只满足一个表达式,这个切点就生效

advisor 执行的时间

around > before > procceed > around > after > afterReturning 正常执行顺序

img

around > before > procceed (exception)> after > afterThrowing

img

多个同一个方法有多个切点的执行顺序

pointCut1-around > pointCut1- before > pointCut2-around > pointCut2-before > procceed > pointCut1-around>pointCut2-around >pointCut1-after>pointCut2-after

一般一个aspect类中定义一个切点,我们可以控制aspect Bean创建先后顺序指定哪个切点的advisor先执行(使用@Order(num)注解,num越小则优先级越高)