package com.dch.service.aop; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * AOP Controller RestController * * @author liu wp * */ @Aspect @Component public class AutoControllerAspect { private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** 日誌類 */ private final Logger logger = LoggerFactory.getLogger(super.getClass()); /** * AfterReturning 核心業務邏輯調用正常退出後,不論是否有返回值,正常退出後,均執行 * * @param joinPoint * @param returnObj * 返回值 */ @AfterReturning(pointcut = "allControllerMethod()||allrestControllerMethod()", returning = "returnObj") public void afterReturning(final JoinPoint joinPoint, final Object returnObj) { logger.info("----afterReturning Start 請求時間:{}----", dateTimeFormat.format(new Date())); logger.info("返回值:{}", returnObj); logger.info("----afterReturning End 請求時間:{}----", dateTimeFormat.format(new Date())); } /** * AfterThrowing 核心業務邏輯調用異常退出後執行,處理錯誤信息 * * @param joinPoint * @param ex * 異常信息 */ @AfterThrowing(value = "allControllerMethod()||allrestControllerMethod()", throwing = "ex") public void AfterThrowing(final JoinPoint joinPoint, final Exception ex) { logger.info("----AfterThrowing Start 請求時間:{}---", dateTimeFormat.format(new Date())); logger.info("異常信息:{}", ex.getMessage()); logger.info("----AfterThrowing End 請求時間:{}---", dateTimeFormat.format(new Date())); } /** * 監控全部@Controller的方法 */ @Pointcut("within(@org.springframework.stereotype.Controller *)") public void allControllerMethod() { } @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)") public void allrestControllerMethod() { } /** * Around 手動控制調用核心業務邏輯,以及調用前和調用後的處理, * * 注意:當核心業務拋異常後,當即退出,轉向afterReturning 執行完afterReturning,再轉到AfterThrowing * * @param joinPoint * @return * @throws Throwable */ @Around("allControllerMethod()||allrestControllerMethod()") public Object Around(final ProceedingJoinPoint joinPoint) throws Throwable { logger.info("----Around Start 請求時間:{}----", dateTimeFormat.format(new Date())); logger.info("---請求前 beforeMethod"); final Object obj = joinPoint.proceed(); logger.info("---請求後 afterReturning"); logger.info("----Around End 請求時間:{}----", dateTimeFormat.format(new Date())); return obj; } /** * 在覈心業務執行前執行。 * * @param joinPoint * @throws InterruptedException */ @Before("allControllerMethod() || allrestControllerMethod()") public void beforeMethod(final JoinPoint joinPoint) throws InterruptedException { logger.info("--beforeMethod Start 請求時間:{}--", dateTimeFormat.format(new Date())); final ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); final HttpServletRequest request = servletRequestAttributes.getRequest(); final String requestUrl = request.getRequestURI().toString(); final int jpsHashCode = joinPoint.getSignature().hashCode(); logger.info("訪問序號:{},請求路徑:{},訪問請求參數對象 : {}", jpsHashCode, requestUrl, Arrays.toString(joinPoint.getArgs())); logger.info("--beforeMethod End 請求時間:{}--", dateTimeFormat.format(new Date())); } }