最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringAOP實(shí)現(xiàn)日志收集管理功能(步驟詳解)

 更新時(shí)間:2022年03月31日 15:20:59   作者:張泓銳  
這篇文章主要介紹了SpringAOP實(shí)現(xiàn)日志收集管理功能,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

第一步引入必要的依賴

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.15</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>

第二步創(chuàng)建自定義日志注解類

package com.example.aop.log.annotion;
import java.lang.annotation.*;
/**
 * @author zhr_java@163.com
 * @date 2022/3/29 20:56
 */
@Inherited
@Target({ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface LogNotice {}

第三步切面設(shè)計(jì),掃描上面的注解

package com.example.aop.log.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
 * @author zhr_java@163.com
 * @date 2022/3/29 20:59
 */
@Aspect
@Component
public class LogAspect {
  @Pointcut("@annotation(com.example.aop.log.annotion.LogNotice)")
  public void doLog() {}
  @Around("doLog()")
  public Object insertLog(ProceedingJoinPoint joinPoint) throws Throwable {
    Object object = joinPoint.proceed();
    try {
      HashMap paramsMap;
      Signature signature = joinPoint.getSignature();
      Method method = ((MethodSignature) signature).getMethod();
      String methodName = method.getName();
      String className = method.getDeclaringClass().getName();
      paramsMap = (HashMap) getParams(joinPoint, method);
      // 打印日志,可以記錄到數(shù)據(jù)庫(kù)的日志表里面,等到有客戶反饋的時(shí)候好去查詢?nèi)罩?
      System.out.println(
          "methodName: " + methodName + "; className: " + className + "; paramsMap:" + paramsMap);
      // 也可以記錄請(qǐng)求當(dāng)前接口耗費(fèi)的時(shí)間
    } catch (Exception e) {
      // 如果在日志記錄的過程中出現(xiàn)問題,那么要處理一下異常,不要直接拋出,此處做成消息隊(duì)列通知或者打印日志
    }
    // 此處要進(jìn)行返回代理的對(duì)象,如果沒有返回的話,前端是收不到數(shù)據(jù)的
    return object;
  }
  private Map<String, Object> getParams(ProceedingJoinPoint joinPoint, Method method) {
    // get parameter names
    String[] parameterNames = new DefaultParameterNameDiscoverer().getParameterNames(method);
    Object[] args = joinPoint.getArgs();
    Map<String, Object> params = new HashMap<>(8);
    if (parameterNames != null && parameterNames.length != 0) {
      for (int i = 0; i < parameterNames.length; i++) {
        params.put(parameterNames[i], args[i]);
      }
    return params;
}

第四步把自定義的注解放到你的接口上面

package com.example.demo.controller;
import com.example.aop.log.annotion.LogNotice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author zhr_java@163.com
 * @date 2022/3/29 20:50
 */
@RestController
@RequestMapping("/person")
public class PersonController {
  //在此處加上你自定義的注解(@LogNotice)
  @LogNotice
  @GetMapping("get_person")
  public String getPerson(Integer numbers) {
    return "123";
  }
}

第五步測(cè)試

到此這篇關(guān)于SpringAOP實(shí)現(xiàn)日志收集管理功能的文章就介紹到這了,更多相關(guān)SpringAOP日志收集管理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JPA @Query時(shí),無法使用limit函數(shù)的問題及解決

    JPA @Query時(shí),無法使用limit函數(shù)的問題及解決

    這篇文章主要介紹了JPA @Query時(shí),無法使用limit函數(shù)的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot測(cè)試junit遇到的坑及解決

    SpringBoot測(cè)試junit遇到的坑及解決

    這篇文章主要介紹了SpringBoot測(cè)試junit遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 簡(jiǎn)單了解java ibatis #及$的區(qū)別和用法

    簡(jiǎn)單了解java ibatis #及$的區(qū)別和用法

    這篇文章主要介紹了簡(jiǎn)單了解java ibatis #及$的區(qū)別和用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java集合使用 Iterator 刪除元素

    Java集合使用 Iterator 刪除元素

    這篇文章主要介紹了Java集合使用 Iterator 刪除元素,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Spring boot詳解緩存redis實(shí)現(xiàn)定時(shí)過期方法

    Spring boot詳解緩存redis實(shí)現(xiàn)定時(shí)過期方法

    本篇文章分享的就是spring boot中的一個(gè)輪子,spring cache注解的方式實(shí)現(xiàn)接口數(shù)據(jù)緩存。默認(rèn)的配置想非常簡(jiǎn)單,但是有一個(gè)弊端是緩存數(shù)據(jù)為永久緩存,本次將介紹如何設(shè)置接口緩存數(shù)據(jù)的過期時(shí)間
    2022-07-07
  • Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項(xiàng)目的詳細(xì)步驟

    Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項(xiàng)目的詳細(xì)步驟

    這篇文章主要介紹了Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項(xiàng)目,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • mybatis-plus開啟sql打印的三種方式總結(jié)

    mybatis-plus開啟sql打印的三種方式總結(jié)

    這篇文章主要給大家介紹了mybatisplus開啟sql打印的三種方式,文章通過代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下
    2023-11-11
  • Maven中jar包下載失敗的幾種解決方法

    Maven中jar包下載失敗的幾種解決方法

    本文主要介紹了Maven中jar包下載失敗的幾種解決方法,包括配置國(guó)內(nèi)Maven源、刪除本地jar包目錄重新下載,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-02-02
  • Spring 依賴注入實(shí)現(xiàn)示例

    Spring 依賴注入實(shí)現(xiàn)示例

    這篇文章主要介紹了Spring 依賴注入實(shí)現(xiàn)示例的相關(guān)資料,幫助大家更好的理解和使用spring框架,感興趣的朋友可以了解下
    2020-11-11
  • Java信號(hào)量全解析

    Java信號(hào)量全解析

    這篇文章主要介紹了Java信號(hào)量的相關(guān)資料,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2021-01-01

最新評(píng)論

资溪县| 丹东市| 高尔夫| 望江县| 壶关县| 雷波县| 阿拉善右旗| 新和县| 军事| 长宁县| 郑州市| 临城县| 英德市| 闽侯县| 乌拉特中旗| 子洲县| 平乐县| 崇文区| 青海省| 罗定市| 五家渠市| 隆回县| 环江| 湘潭县| 闻喜县| 鄂托克前旗| 盐池县| 永顺县| 连山| 资溪县| 嘉祥县| 龙陵县| 思茅市| 泽普县| 江孜县| 藁城市| 阳谷县| 上杭县| 盖州市| 保德县| 温泉县|