4
請不要猶豫編輯問題或詢問有關questin的更多細節。在spring中使用aspectJ記錄異常?
我知道可以log
使用aspectJ
作爲以下方法的ArithmeticException
,
public void afterThrowingAspect(){
System.out.println("This is afterThrowingAspect() !");
int i=2/0;
System.out.println("i value : "+i);
}
中aspect類有,
@AfterThrowing(pointcut = "execution(* com.pointel.aop.test1.AopTest.afterThrowingAspect(..))",throwing= "error")
public void logAfterError(JoinPoint joinPoint,Throwable error) {
System.out.println("Hi jacked Method name : " + joinPoint.getSignature().getName());
log.info("Method name : " + joinPoint.getSignature().getName());
log.info("Error report is : " + error);
}
通常,我可以使用TRY
和CATCH
塊處理異常和log
中的每個CATCH
塊中的錯誤爲,
public void someMehtod(){
try{
int i=2/0;
System.out.println("i value : "+i);
}catch{ArithmeticException err){
log.info("The exception you got is : " + err);
}
}
但我不喜歡做的logging
像每一個catch
塊單獨在所有java
類我的項目一樣的,
log.info("The exception you got is : " + err);
我願做logging
內CATCH
塊我的應用程序使用aspectJ
類。
希望你們都明白我的問題。謝謝。
如果定義了@AfterThrowing切入點爲'切入點= 「執行(* com.pointel。*(..))」'你會觸發@AfterThrowing方面每次在com.pointel的包和子包中引發異常。那是你想要的嗎? – Julien
請參閱我編輯的問題。 –
@Julien我不需要記錄'@ AfterThrowing'方法。我需要使用'aspectJ'在catch塊中記錄'exception'。我可能嗎? –