Spring AOP: How to determine if method threw an exception using @After?

為{幸葍}努か 提交于 2019-12-18 09:43:23

问题


Spring docs say:

After advice must be prepared to handle both normal and exception return conditions.

@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doReleaseLock() {
    // ...
}

I'm interested in knowing whether dataAccessOperation completed normally or with an exception. Unfortunately, the above code snippet is in the run for the most useless documentation ever produced. I understand that I can use AfterReturning and AfterThrowing separately, or even Around, but that's not the point. Since After exists, I should be able to use it.


回答1:


What the documentation wants to say is that your @After advice should do a valid thing no matter whether an exception was thrown or not. It does not mean you can determine it from the advice. If different things should be done,

  • either use @AfterReturning vs. @AfterThrowing if the control flow is not to be touched and no exception is to be handled by your advices,
  • or use an @Around advice if you want to actually catch and handle thrown exceptions and/or modify method parameters before call or the return value after the call or skip method execution altogether.


来源:https://stackoverflow.com/questions/48775637/spring-aop-how-to-determine-if-method-threw-an-exception-using-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!