1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        SpringBoot學(xué)習(xí)之整合AOP

        共 7505字,需瀏覽 16分鐘

         ·

        2020-10-15 20:20

        點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”

        優(yōu)質(zhì)文章,第一時(shí)間送達(dá)

        66套java從入門到精通實(shí)戰(zhàn)課程分享

        基于SpringBoot整合Aop記錄日志

        • SpringBoot各版本參考文檔

          https://docs.spring.io/spring-boot/docs/

        • 查找引入依賴


        • 引入依賴




        • ??
          ????org.springframework.boot
          ????spring-boot-starter-aop
          ??





        • 切面類




        • ??package?link.lycreate.springbooteasyexceldemo.aspect;

          ??import?lombok.extern.slf4j.Slf4j;
          ??import?org.aspectj.lang.JoinPoint;
          ??import?org.aspectj.lang.ProceedingJoinPoint;
          ??import?org.aspectj.lang.annotation.*;
          ??import?org.aspectj.lang.reflect.MethodSignature;
          ??import?org.springframework.stereotype.Component;
          ??import?org.springframework.web.context.request.RequestAttributes;
          ??import?org.springframework.web.context.request.RequestContextHolder;
          ??import?org.springframework.web.context.request.ServletRequestAttributes;

          ??import?javax.servlet.http.HttpServletRequest;
          ??import?java.lang.reflect.Method;
          ??import?java.util.Arrays;

          ??/**
          ??*?@ClassName?LogAspect
          ??*?@Description?TODO?日志切面類$
          ??*?@Author?charlesYan
          ??*?@Date?2020/10/9?12:53
          ??*?@Version?1.0
          ??**/
          ??@Aspect??//?聲明是一個(gè)切面組件
          ??@Component?//?加入到IOC容器
          ??@Slf4j??//?等同于?private?final?Logger?logger?=?LoggerFactory.getLogger(XXX.class);
          ??public?class?LogAspect?{

          ??????/**
          ??????*?@Author?charlesYan
          ??????*?@Description?//?指定切入點(diǎn)表達(dá)式,攔截那些方法,即為哪些類生成代理對(duì)象
          ??????*??????第一*表示匹配任何返回值的方法
          ??????*??????第二*表示匹配controller包下的所有類
          ??????*??????第三*表示匹配類下的所有方法
          ??????*??????..表示任何個(gè)數(shù)參數(shù),和如何類型的參數(shù)
          ??????**/
          ??????//@Pointcut("execution(*?link.lycreate.springbooteasyexceldemo.controller.*.*(..))")
          ??????@Pointcut("@annotation(link.lycreate.springbooteasyexceldemo.aspect.LogFilter)")?//在所有標(biāo)記指定注解的方法上攔截
          ??????public?void?logPointCut(){}

          ??????/**
          ??????*?@Author?charlesYan
          ??????*?@Description //前置通知:在目標(biāo)方法執(zhí)行前調(diào)用
          ??????**/
          ??????@Before("logPointCut()")
          ??????public?void?before(JoinPoint?joinPoint){
          ??????????System.out.println("---------------Before?Begin?CurrentTime?=?"?+?System.currentTimeMillis());
          ??????????/*獲取當(dāng)前請(qǐng)求的HttpServletRequest*/
          ??????????RequestAttributes?requestAttributes?=?RequestContextHolder.getRequestAttributes();
          ??????????HttpServletRequest?request?=?((ServletRequestAttributes)requestAttributes).getRequest();

          ??????????log.info("URL-->"+request.getRequestURL().toString());
          ??????????log.info("IP-->"+request.getRemoteAddr());
          ??????????log.info("HTTP_Method-->"+request.getMethod());
          ??????????log.info("Request_args-->"+?Arrays.toString(joinPoint.getArgs()));

          ??????????System.out.println("---------------Before?End?CurrentTime?=?"?+?System.currentTimeMillis());

          ??????}

          ??????/**
          ??????*?@Author?charlesYan
          ??????*?@Description?//返回通知?在目標(biāo)方法執(zhí)行后調(diào)用,若目標(biāo)方法出現(xiàn)異常,則不執(zhí)行
          ??????**/
          ??????@AfterReturning(value?=?"logPointCut()",returning?=?"obj")
          ??????public?void?afterReturning(JoinPoint?joinPoint,Object?obj){
          ??????????System.out.println("---------------AfterReturn?CurrentTime?=?"?+?System.currentTimeMillis());

          ??????}

          ??????/**
          ??????*?@Author?charlesYan
          ??????*?@Description //后置通知:無(wú)論目標(biāo)方法在執(zhí)行過(guò)程中是否出現(xiàn)異常都會(huì)在它之后調(diào)用
          ??????**/
          ??????@After("logPointCut()")
          ??????public?void?after(JoinPoint?joinPoint){
          ??????????System.out.println("---------------After?CurrentTime?=?"?+?System.currentTimeMillis());
          ??????}

          ??????/**
          ??????*?@Author?charlesYan
          ??????*?@Description //異常通知:目標(biāo)方法拋出異常時(shí)執(zhí)行
          ??????**/
          ??????@AfterThrowing(value?=?"logPointCut()",?throwing?=?"ex")
          ??????public?void?afterThrowing(JoinPoint?joinPoint,Exception?ex){
          ??????????System.out.println("---------------AfterThrowing?CurrentTime?=?"?+?System.currentTimeMillis());
          ??????}

          ??????/**
          ??????*?@Author?charlesYan
          ??????*?@Description //環(huán)繞通知:是前面四個(gè)通知的結(jié)合體
          ??????*?????????需要在方法之前執(zhí)行,可以寫在joinPoint.procedd();之前
          ??????*?????????需要在方法之后執(zhí)行,可以寫在joinPoint.procedd();之后
          ??????**/
          ??????@Around("logPointCut()")
          ??????public?void?around(ProceedingJoinPoint?joinPoint)?throws?Throwable?{

          ??????????//?獲取目標(biāo)方法的名稱
          ??????????String?methodName?=?joinPoint.getSignature().getName();
          ??????????//?獲取方法傳入?yún)?shù)
          ??????????Object[]?params?=?joinPoint.getArgs();
          ??????????MethodSignature?signature?=?(MethodSignature)?joinPoint.getSignature();
          ??????????Method?method?=?signature.getMethod();
          ??????????//?獲取方法上LogFilter注解
          ??????????LogFilter?logFilter?=?method.getAnnotation(LogFilter.class);
          ??????????String?value?=?logFilter.value()?;
          ??????????log.info("模塊描述:"+value);
          ??????????System.out.println("---------------Around?Before?CurrentTime?=?"?+?System.currentTimeMillis()?+?" method name:"?+?methodName?+?" args:"?+?params[0]);
          ??????????//?執(zhí)行源方法
          ??????????joinPoint.proceed();
          ??????????System.out.println("---------------Around?After?CurrentTime?=?"?+?System.currentTimeMillis()?+?" method name:"?+?methodName?+?" args:"?+?params[0]);
          ??????}
          ??}





        • 自定義注解



        ??/**
        ??*?@ClassName?LogFilter
        ??*?@Description?TODO?自定義日志注解類$
        ??*?@Author?charlesYan
        ??*?@Date?2020/10/11?17:59
        ??*?@Version?1.0
        ??**/

        ??@Target(ElementType.METHOD)//Target注解決定LogFilter注解可以加在哪些成分上,如加在類身上,或者屬性身上,或者方法身上等成分
        ??@Retention(RetentionPolicy.RUNTIME)//Retention注解括號(hào)中的"RetentionPolicy.RUNTIME"意思是讓LogFilter這個(gè)注解的生命周期一直程序運(yùn)行時(shí)都存在
        ??@Documented
        ??public?@interface?LogFilter?{

        ??????String?value()?default?"";
        ??}




        • 請(qǐng)求方法


        ??@LogFilter("保存請(qǐng)求日志")
        ??@RequestMapping(path?=?"/saveRequestLog",method?=?RequestMethod.POST)
        ??public?String?saveRequestLog(@RequestParam("name")String?name){
        ??????return?"請(qǐng)求成功:"?+?name;
        ??}



        • 測(cè)試方式

        報(bào)錯(cuò)信息

        方法參數(shù)未聲明

        • 報(bào)錯(cuò)信息



        • ??Caused?by:?java.lang.IllegalArgumentException:?error?at?::0?formal?unbound?in?pointcut



        • 截圖信息

        • 錯(cuò)誤代碼


        ??/**
        ??*?@Author?charlesYan
        ??*?@Description?//返回通知?在目標(biāo)方法執(zhí)行后調(diào)用,若目標(biāo)方法出現(xiàn)異常,則不執(zhí)行
        ??**/
        ??@AfterReturning("logPointCut()")
        ??public?void?afterReturning(JoinPoint?joinPoint,Object?obj){
        ??????System.out.println("---------------AfterReturn?CurrentTime?=?"?+?System.currentTimeMillis());

        ??}


        • 正確代碼


        ??/**
        ??*?@Author?charlesYan
        ??*?@Description?//返回通知?在目標(biāo)方法執(zhí)行后調(diào)用,若目標(biāo)方法出現(xiàn)異常,則不執(zhí)行
        ??**/
        ??@AfterReturning(value?=?"logPointCut()",?returning?=?"obj")
        ??public?void?afterReturning(JoinPoint?joinPoint,Object?obj){
        ??????System.out.println("---------------AfterReturn?CurrentTime?=?"?+?System.currentTimeMillis());

        ??}



        原因分析


        ??新增的方法參數(shù)未賦值



        總結(jié)

        Aop切面通知執(zhí)行順序

        • 例圖

        通知注解中的value屬性補(bǔ)充

        • 自定義注解



        • ??//注解實(shí)體類
          ??package?com.trip.demo;

          ??import?java.lang.annotation.ElementType;
          ??import?java.lang.annotation.Retention;
          ??import?java.lang.annotation.RetentionPolicy;
          ??import?java.lang.annotation.Target;


          ??@Retention(RetentionPolicy.RUNTIME)
          ??@Target({?ElementType.METHOD?})
          ??public?@interface?SMSAndMailSender?{

          ??????/*短信模板String格式化串*/
          ??????String?value()?default?"";

          ??????String?smsContent()?default?"";

          ??????String?mailContent()?default?"";
          ??????/*是否激活發(fā)送功能*/
          ??????boolean?isActive()?default?true;
          ??????/*主題*/
          ??????String?subject()?default?"";

          ??}





        • 切面類中的切面方法

        • ??/**
          ??*?在所有標(biāo)記了@SMSAndMailSender的方法中切入
          ??*?@param?joinPoint
          ??*?@param?obj
          ??*/
          ??@AfterReturning(value="@annotation(com.trip.demo.SMSAndMailSender)",?returning="obj")
          ??public?void?afterReturning(JoinPoint?joinPoint,Object?obj){
          ??????System.out.println("---------------AfterReturn?CurrentTime?=?"?+?System.currentTimeMillis());

          ??}



        參考鏈接

        • SpringBoot整合Aop

          https://www.cnblogs.com/fernfei/p/12092510.html

        • SpringBoot整合aop

          https://www.cnblogs.com/myitnews/p/11848159.html

        • SpringBoot 通過(guò)自定義注解實(shí)現(xiàn)AOP切面編程實(shí)例

          https://www.cnblogs.com/lingyejun/p/9941350.html

        • SpringBoot2.0 基礎(chǔ)案例(11):配置AOP切面編程,解決日志記錄業(yè)務(wù)

          https://my.oschina.net/cicadasmile/blog/3073069

        • 淺談Spring AOP 面向切面編程 最通俗易懂的畫(huà)圖理解AOP、AOP通知執(zhí)行順序~

          https://www.cnblogs.com/ChromeT/p/11823735.html

        • @interface 注解詳解

          https://blog.csdn.net/u010882691/article/details/82427520




        版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。

        本文鏈接:

        https://blog.csdn.net/weixin_42586723/article/details/109017827






        ??? ?



        感謝點(diǎn)贊支持下哈?

        瀏覽 51
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            99热这里只有精品首页 | 国产精品久久成人 | 国产一线二线视频 | 五月丁香色亭亭激情视频 | 豆花视频黄网站在线观看 | 青娱乐视视频 | 国产伦久久久精品A88 | 插逼逼网站 | 久热国产在线视频 | 韩国操逼网 |