我使用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("汪汪汪");
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.