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