我使用spring-boot-starter-parent 2.7.4测试了一下,得到的结论是这样的:
(我的切点的方法就是输出“汪汪汪”)
不使用@Around通知:
before ......
汪汪汪
afterReturning ......
after ......
使用@Around通知:
around before......
before ......
汪汪汪
afterReturning ......
after ......
around after......
-
按照网上的说法,应该顺序是这样的:
around before->before->切点方法->around after->after->after returning -
为什么我的测试结果中
afterReturning竟然还跑到after的前面去了,around after跑到最后面去了?
我的@Aspect代码`
@Aspect
public class MyAspect {
@Pointcut("execution(* com.example.springboot_test_1005.Dog.bark(..))")
public void pointCut() {
}
@Before("pointCut()")
public void before() {
System.out.println("before ......");
}
@After("pointCut()")
public void after() {
System.out.println("after ......");
}
@AfterReturning("pointCut()")
public void afterReturning() {
System.out.println("afterReturning ......");
}
@AfterThrowing("pointCut()")
public void afterThrowing() {
System.out.println("afterThrowing ......");
}
@Around("pointCut()")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("around before......");
// 回调目标对象的原有方法
jp.proceed();
System.out.println("around after......");
}
}
被代理的类:
@Component
public class Dog {
void bark(){
System.out.println("汪汪汪");
}
}