为什么@AfterReturning仅在异常时@AfterThrowing之后执行?
我有以下内容:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你想在方法之后执行代码,无论是否抛出异常,你需要使用
@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.