问题
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