开发者

Why is @AfterReturning only executed after @AfterThrowing on exception?

开发者 https://www.devze.com 2023-01-27 05:22 出处:网络
I have the following: @AfterReturning(\"executionOfTrustedAnnotatedMethod()\") public void afterReturningFromTrustedMethodExecution() { ... }

I have the following:

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

@AfterThrowing(pointcut = "executionOfTrustedAnnotatedMethod()")
public void afterThrow开发者_如何学CingByExecutionOfTrustedAnnotatedMethod() { ... }

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消