基本配置使用:java
1. 在 pom.xml 中增長基礎類庫引用web
<!-- HH: 引入 spring-boot-aop 模塊 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2. 配置文件 application.yml 中增長參數配置spring
spring: application: name: HAVENT-SPRING-BOOT-DEMO aop: auto: true #proxy-target-class: true
3. 新增 KafkaLogAspect.java 攔截器類app
package com.havent.demo.aop.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Arrays; @Aspect @Component public class KafkaLogAspect { /** * 環繞通知:目標方法執行先後分別執行一些代碼,發生異常的時候執行另一些代碼 * @return */ @Around(value="execution(* com.havent.demo..*(..))") public Object aroundMethod(ProceedingJoinPoint joinPoint){ long beginTime = System.currentTimeMillis(); //請求的方法名 String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); String loggerType = className + "." + methodName; Object result = null; try { System.out.println("【前置通知】:the method 【" + loggerType + "】 begins with " + Arrays.asList(joinPoint.getArgs())); //執行目標方法 result = joinPoint.proceed(); System.out.println("【返回通知】:the method 【" + loggerType + "】 ends with " + result); } catch (Throwable e) { System.out.println("【異常通知】:the method 【" + loggerType + "】 occurs exception " + e); } // 執行時長(毫秒) long time = System.currentTimeMillis() - beginTime; System.out.println("【後置通知】:-----------------end.---------------------- time:" + time); return result; } }
注意代碼中 execution(* com.havent.demo..*(..)) 表示監控範圍爲 com.havent.demo 下面的全部方法spring-boot
調用 com.havent.demo 下面的方法,捕獲日誌以下:spa
進階操做,監控 controller 操做,獲取 web 請求信息:3d
package com.havent.demo.aop.aspect; import com.havent.demo.logger.service.KafkaService; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; @Aspect @Component public class KafkaLogAspect { @Autowired private KafkaService logger; @Pointcut("execution(public * com.havent.demo.controller.*.*(..))") public void webLog(){} @Before("webLog()") public void deBefore(JoinPoint joinPoint) throws Throwable { // 接收到請求,記錄請求內容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 記錄下請求內容 System.out.println("URL : " + request.getRequestURL().toString()); System.out.println("HTTP_METHOD : " + request.getMethod()); System.out.println("IP : " + request.getRemoteAddr()); System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs())); //logger.trace(""); } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) throws Throwable { // 處理完請求,返回內容 System.out.println("方法的返回值 : " + ret); //logger.trace(""); } //後置異常通知 @AfterThrowing("webLog()") public void throwss(JoinPoint jp){ System.out.println("方法異常時執行....."); //logger.trace(""); } //後置最終通知,final加強,無論是拋出異常或者正常退出都會執行 @After("webLog()") public void after(JoinPoint jp){ System.out.println("方法最後執行....."); } //環繞通知,環繞加強,至關於MethodInterceptor @Around("webLog()") public Object arround(ProceedingJoinPoint pjp) { System.out.println("方法環繞start....."); try { Object o = pjp.proceed(); System.out.println("方法環繞proceed,結果是 :" + o); return o; } catch (Throwable e) { e.printStackTrace(); return null; } } }
執行結果以下:日誌