日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

aop记录轨迹

發布時間:2024/9/27 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aop记录轨迹 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

            • 一、常用的場景
            • 1. 請求攔截
            • 2. 異步保存軌跡
            • 二、案例實戰
            • 2.1. pom
            • 2.2. 自定義注解
            • 2.3. aop攔截
            • 2.4. 測試類
            • 2.5. 保存日志

一、常用的場景
1. 請求攔截

通過aop 請求攔截,舉個例子,第三方廠商請求平臺接口,先去數據庫查詢該接口,此ip是否有訪問權限,有如果就通過,繼續下面的邏輯,否則,權限訪問攔截,請求到此結束!

2. 異步保存軌跡

見下面案例:說一下思路
也是同理,同樣通過攔截器來實現的,利用下的注解即可,案例中柚子
@Aspect
@Pointcut(“execution( * com.gblfy.logboot...*(…))”)//兩個…代表所有子目錄,最后括號里的兩個…代表所有參數
@After
@Around

二、案例實戰
2.1. pom
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency>
2.2. 自定義注解
package com.gblfy.logboot.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME) //注解作用的位置,ElementType.METHOD表示該注解僅能作用于方法上 @Target(ElementType.METHOD) public @interface Log {String value() default ""; }
2.3. aop攔截
package com.gblfy.logboot;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 WebLogAspect {private static final Logger logger = LoggerFactory.getLogger(WebLogAspect.class);@AutowiredHttpServletRequest request;@Pointcut("execution( * com.gblfy.logboot.*.*.*(..))")//兩個..代表所有子目錄,最后括號里的兩個..代表所有參數public void logPointCut() {}@Before("logPointCut()")public void doBefore(JoinPoint joinPoint) throws Throwable {// 接收到請求,記錄請求內容ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();// 記錄下請求內容logger.info("請求地址 : " + request.getRequestURL().toString());logger.info("HTTP METHOD : " + request.getMethod());// 獲取真實的ip地址//logger.info("IP : " + WebUtils.getIpAddress(request));logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "."+ joinPoint.getSignature().getName());logger.info("參數 : " + Arrays.toString(joinPoint.getArgs())); // loggger.info("參數 : " + joinPoint.getArgs());}@After("logPointCut()")public void doAfter(JoinPoint joinPoint) throws Throwable {System.out.println("request---" + request.getAttribute("aa"));}@Around("logPointCut()")public Object doAround(ProceedingJoinPoint pjp) throws Throwable {long startTime = System.currentTimeMillis();Object ob = pjp.proceed();// ob 為方法的返回值logger.info("耗時 : " + (System.currentTimeMillis() - startTime));return ob;} }
2.4. 測試類
package com.gblfy.logboot.controller;import com.gblfy.logboot.annotation.Log; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;@RequestMapping("/log") @RestController("日志控制器") public class LogController {@Log("測試收集日志")@RequestMapping("/save")public String saveLog(@RequestParam(name = "token") String token,@RequestParam(name = "name") String name,HttpServletRequest request, HttpServletResponse response) {System.out.println("開始收集log"+token+name);request.setAttribute("aa","assddddd");return "收集日志succes333s";} // http://localhost:8080/log/save?token=123 }
2.5. 保存日志

總結

以上是生活随笔為你收集整理的aop记录轨迹的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。