Exception handling and after advice

浪子不回头ぞ 提交于 2019-12-20 07:27:12

问题


I am using AOP with spring boot. After some method execution successfully I am using @After advice in AOP to do some data base insertion. There is one case if the method throw an exception somewhere then I don't want to execute my @After advice call.

I don't have any idea if I catch exception in AOP also my after advise method will going to execute.

@After(value = "execution(* saveUpdateMeasures(..)) and args(addMeasure)")
public void afterAdviseMeasure(JoinPoint joinPoint,AddMeasures addMeasure) throws Exception {
    logger.info("url is " + request.getRequestURL() + "?"  + request.getQueryString()); 
    saveUserLog(addMeasure.getUserId(), "add update measure",addMeasure.getReviewId()); 
}

So if my method saveUpdateMeasures() executed successfully without any exception then only I am looking for executing afterAdviseMeasure. Any help will be appreciated.


回答1:


Please read the Spring AOP manual, especially the chapter about advice types. In general it is always a good idea to read a manual before using a new technology. ;-)

  • @After is always executed after a method terminates, no matter if there was an exception or not. More specialised versions only run
  • @AfterThrowing an exception or
  • @AfterReturning regularly. The latter is what you are looking for and it even gives you access to the return value if you like to log it or so.

None of these advice types will let you handle the exception, though, as I also explain here. You need to use an @Around advice for that.



来源:https://stackoverflow.com/questions/56558463/exception-handling-and-after-advice

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