为什么@AfterReturning仅在异常时@AfterThrowing之后执行?

发布于 2024-10-05 04:21:24 字数 606 浏览 1 评论 0原文

我有以下内容:

@AfterReturning("executionOfTrustedAnnotatedMethod()")
public void afterReturningFromTrustedMethodExecution() { ... }

@AfterThrowing(pointcut = "executionOfTrustedAnnotatedMethod()")
public void afterThrowingByExecutionOfTrustedAnnotatedMethod() { ... }

我观察到这种行为对我来说没有意义:

  • 如果此切入点捕获的方法不抛出异常,则执行 @AfterReturning
  • 如果该方法抛出异常,则仅在 @AfterThrowing 时执行 @AfterReturning存在并首先执行

我想要完成的是在方法执行结束时运行一些代码,无论是否抛出异常。但现在如果抛出异常,这段代码会运行两次(如果我同时有 afterReturning 和 afterThrowing),或者根本不运行(如果我只有 afterReturning)。

有什么建议吗?

谢谢, 皮奥特尔

I have the following:

@AfterReturning("executionOfTrustedAnnotatedMethod()")
public void afterReturningFromTrustedMethodExecution() { ... }

@AfterThrowing(pointcut = "executionOfTrustedAnnotatedMethod()")
public void afterThrowingByExecutionOfTrustedAnnotatedMethod() { ... }

And Im observing this behaviour which does not make sense to me:

  • If the method captured by this pointcut does not throw an exception, @AfterReturning is executed
  • If the method throws an exception, @AfterReturning is only executed if @AfterThrowing exists and is executed first

What I am trying to accomplish is to run some code at the end of the execution of a method regardless if there was an exception thrown or not. But now this code runs twice (if I have both afterReturning and afterThrowing) or not at all (if I only have afterReturning) if there is an exception thrown.

Any suggestions?

Thanks,
Piotr

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网白 2024-10-12 04:21:24

如果你想在方法之后执行代码,无论是否抛出异常,你需要使用 @After

If you want to execute a code after method regardless if there was an exception thrown or not, you need to use @After instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文