一、Advice执行顺序
如果多个Advice和同一个JointPoint连接,我们需要指明每一个Advice的执行顺序,那么我们就需要分两种情况讨论,
- 第一种是如果Advice在同一个Aspect当中,那么执行的顺序是按照Advice在Aspect的声明顺序执行的。
- 如果Advice在不同的Aspect当中,那么执行的顺序是需要将Aspect继承Ordered这个类,然后覆写getOrder方法
二、Advice在同一个Aspect中
AdviceOrder.java 在这个Aspect中我们指明了Pointcut和Advice的类型,我们可以看到,两个Before类型的Advice切入到了同一个JointPoint上,两个AfterReturning类型的Advice切入到了同一个JointPoint上。
package com.zbt.day04;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AdviceOrder {
@Pointcut("execution(* com.zbt.day04.*.*(..))")
public void methodPoint(){
}
@Before("methodPoint()")
public void beforeAdviceI(){
System.out.println("before advice I 执行了");
}
@Before("methodPoint()")
public void beforeAdviceII(){
System.out.println("before advice II 执行了");
}
@AfterReturning("methodPoint()")
public void afterReturningI(){
System.out.println("After Returning Advice I 执行了");
}
@AfterReturning("methodPoint()")
public void afterReturningII(){
System.out.println("After Returning Advice I 执行了");
}
}
TargetClass.java 这是一个目标类,其中的sayHello方法是JointPoint
package com.zbt.day04;
public class TargetClass {
public void sayHello(){
System.out.println("Hello,What a good day!");
}
}
XML配置文档
<?xml version="1.0" encoding="UTF-8&