AOP

Log

通常Log皆由AOP來執行
透過AOP將繁雜的LOG從商業邏輯獨立出來

log.error("message",ex);
會記錄stacktace
(在捕捉例外之後 ,必須讓新拋出的Exception帶入捕捉的ex才有辦法紀錄原本的stackTrace) ------------------------------------------------------------------------------------------------;

ExceptionHandler

透過AOP,針對拋出的Exception能做統一的處理 --------------------------------------------------------------------------------------------------;

CutPoint

(若cutpoint點是在contoller層,則不能再controller層 try catch,否則無法觸發@AfterThrowing)

@AfterThrowing(pointcut="pointCut()", throwing= "ex")
public void afterThrowingAdvice(JoinPoint joinPoint, Throwable ex){
    ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder
        .getRequestAttributes();

    if( servletRequestAttributes != null ) {
        String method = servletRequestAttributes.getRequest().getMethod();
        String payload = getPayload(joinPoint);
        log.error("Method:" + method);
        log.error("PAYLOAD:" + payload);
        log.error("stacktace",ex);
        //
        //log.error(ExceptionUtils.getStackTrace(error));
    }
}