2017-07-17 29 views
0

我知道我可以使用@AfterReturning@AfterThrowing建议来检查一个方法是否返回结果或异常,但如果我想使用@After建议?AOP:检查方法是否返回了异常或不是使用后建议

有没有办法来检查方法返回的东西或异常使用@After建议?

+0

不,你不能这样做。为了这些目的,您应该使用'@ AfterReturning'或'@ AfterThrowing'。 – fg78nc

回答

0

@After这是建议,适用于正常情况或案例与例外,无分离。

@AfterReturning - 如果方法完成没有例外。

@AfterThrowing - 在方法完成时的例外。

在您需要了解方法完成时,您可以使用@Around建议来完成。

@Around("pointcut") 
public Object execute(ProceedingJoinPoint pjp) throws Throwable {   
    try {     
     Object result = pjp.proceed(); 
     //here you know that method is completed without exception 
     return result; 
    } 
    catch(Throwable ... Exception ..... ex) { 
     //here you know that method throws exception 
     log() , wrap() , handle()..... whatever.... 
    } 
} 
相关问题