日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

SpringBoot面向切面编程-用AOP方式管理日志

發(fā)布時間:2025/4/5 73 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot面向切面编程-用AOP方式管理日志 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

面向切面編程

認識AOP
AOP(Aspect Oriented Program,面向切面編程)把業(yè)務功能分為核心、非核心兩部分。

核心業(yè)務功能非核心業(yè)務功能
用戶登錄,增加數(shù)據(jù),刪除數(shù)據(jù)性能統(tǒng)計,日志,事務管理

在Spring 的面向切面編程(AOP)思想里,非核心業(yè)務功能被定義為切面。核心業(yè)務功能和切面功能先被分別進行獨立開發(fā),然后把切面功能和核心業(yè)務功能編織在一起,這就是AOP。

AOP中的概念

  • 切入點(pointcut):在哪些類、哪些方法上切入
  • 通知(advice):在方法前、方法后、方法前后做什么
  • 切面(aspect):切面=切入點+通知。也就是在什么時機、什么地方,做什么
  • 織入(weaving):把切面加入對象,并創(chuàng)建出代理對象的過程
  • 環(huán)繞通知:AOP中最強大、靈活的通知,它集成了前置和后置通知,保留了連接點原有的方法。
  • 實例:用AOP方式管理日志

    實驗結果
    登錄網(wǎng)址

    http://localhost:8080/aoptest

    查看到

    同時控制臺顯示信息

    URL:http://localhost:8080/aoptestHTTP方法:GETIP地址:0:0:0:0:0:0:0:1類的方法:com.example.demo.controller.AopLogController.aVoid參數(shù):null應答值:hello aop test費時:12

    pom.xml添加依賴
    這里初學者踩坑:配置切面@Aspect不管用,是因為沒有下面的spring-boot-starter-aop依賴

    <!-- SpringBoot 攔截器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency>

    代碼
    編寫AOP日志注解類
    AopLog.java
    下面代碼的解釋:
    @Before:在切入點開始處切入內(nèi)容
    @After:在切入點結尾處切入內(nèi)容
    @AfterReturning:在切入點返回內(nèi)容之后切入內(nèi)容,可以用來對處理返回值做一些加工處理
    @Around:在切入點前后切入內(nèi)容,并控制何時執(zhí)行切入點自身的內(nèi)容
    @AfterThrowing:用來處理當切入內(nèi)容部分拋出異常之后的處理邏輯
    @Aspect:標記為切面類
    @Component:把切面類加入IoC容器中,讓Spring來進行管理

    package com.example.demo.aop;import org.apache.commons.lang3.builder.ToStringBuilder; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*;//這里包含.Aspect 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;import javax.annotation.processing.SupportedSourceVersion; import javax.servlet.http.HttpServletRequest; import java.util.Arrays;@Aspect//使之成為切面類@Component//把切面類加入loC容器中public class AopLog {private Logger logger=LoggerFactory.getLogger(this.getClass());//線程局部的變量,用于解決多線程中相同變量的訪問沖突問題ThreadLocal<Long> startTime=new ThreadLocal<>();//定義切點@Pointcut("execution(public * com.example..*.*(..))")public void aopWebLog(){}@Before("aopWebLog()")public void doBefore(JoinPoint joinPoint) throws Throwable{startTime.set(System.currentTimeMillis());//接收到請求,記錄請求內(nèi)容ServletRequestAttributes attributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();HttpServletRequest request=attributes.getRequest();//記錄下請求內(nèi)容logger.info("URL:"+request.getRequestURL().toString());logger.info("HTTP方法:"+request.getMethod());logger.info("IP地址:"+request.getRemoteAddr());logger.info("類的方法:"+joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());logger.info("參數(shù):"+request.getQueryString());}@AfterReturning(pointcut = "aopWebLog()",returning = "retObject")public void doAfterReturning(Object retObject) throws Throwable{//處理完請求,返回內(nèi)容logger.info("應答值:"+retObject);logger.info("費時:"+(System.currentTimeMillis()-startTime.get()));}//方法拋出異常退出時執(zhí)行的通知@AfterThrowing(pointcut = "aopWebLog()",throwing = "ex")public void addAfterThrowingLogger(JoinPoint joinPoint,Exception ex){logger.error("執(zhí)行 "+" 異常",ex);} }

    2.控制器
    AopLogController.java

    package com.example.demo.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class AopLogController {@GetMapping("/aoptest")public String aVoid(){return "hello aop test";} }

    總結

    以上是生活随笔為你收集整理的SpringBoot面向切面编程-用AOP方式管理日志的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。