<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          springboot自定義注解,怎么搞?

          共 4299字,需瀏覽 9分鐘

           ·

          2020-09-18 14:07

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

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

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

          Java枚舉

          通常springboot的應(yīng)用場(chǎng)景為:

          • 日志記錄: 記錄請(qǐng)求信息的日志, 以便進(jìn)行信息監(jiān)控, 信息統(tǒng)計(jì), 計(jì)算PV(page View)等

          • 性能監(jiān)控;

          • 權(quán)限檢查;

          • 通用行為

          0. pom引入依賴


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


          1. 自定義注解的范式

          • @Target 注解
            主要說(shuō)明注解的使用范圍,主要包括以下幾種類型:

          TYPE:類,接口或者枚舉
          FIELD:域,包含枚舉常量
          METHOD:方法
          PARAMETER:參數(shù)
          CONSTRUCTOR:構(gòu)造方法
          LOCAL_VARIABLE:局部變量
          ANNOTATION_TYPE:注解類型
          PACKAGE:包

          • @Retention 注解
            主要說(shuō)明注解的生命周期,主要包含以下幾種類型:

          SOURCE:源碼級(jí)別保留,編譯后即丟棄
          CLASS:編譯級(jí)別保留,編譯后的class文件中存在,在jvm運(yùn)行時(shí)丟棄,這是默認(rèn)值。
          RUNTIME:?運(yùn)行級(jí)別保留,編譯后的class文件中存在,在jvm運(yùn)行時(shí)保留,可以被反射調(diào)用。

          • @Documented 注解
            主要指明修飾的注解,可以被例如javadoc此類的工具文檔化,只負(fù)責(zé)標(biāo)記,沒(méi)有成員取值

          2. 具體實(shí)現(xiàn)

          現(xiàn)在我們自定義一個(gè)注解,實(shí)現(xiàn)的功能是當(dāng)系統(tǒng)執(zhí)行某個(gè)函數(shù)時(shí),我們打印請(qǐng)求頭和登錄人的相關(guān)信息。

          2.1 定義接口

          @Target(ElementType.METHOD)??//在方法上使用
          @Retention(RetentionPolicy.RUNTIME)???//運(yùn)行級(jí)別保留
          public?@interface?DetailRequest?{
          ????String?value()?default?"";????//使用注解的時(shí)候傳遞,默認(rèn)為""
          }

          2.2 切面編程

          @Component?????
          @Aspect
          @Slf4j
          public?class?MyAspect?{
          ????
          ????ThreadLocal?currentTime?=?new?ThreadLocal<>();
          ????
          ????public?MyAspect()?{???//驗(yàn)證是否啟動(dòng)時(shí)加載
          ????????System.out.println("==========MyAspect?start=======================");
          ????}

          ????/**
          ?????*?配置切入點(diǎn)
          ?????*/
          ????@Pointcut("@annotation(me.zhengjie.annotation.DetailRequest)")???//此處對(duì)應(yīng)之前我們定義的接口
          ????public?void?detailPointcut()?{
          ????????//?該方法無(wú)方法體,主要為了讓同類中其他方法使用此切入點(diǎn)
          ????}


          ????/**
          ?????*?配置環(huán)繞通知,使用在方法logPointcut()上注冊(cè)的切入點(diǎn)
          ?????*
          ?????*?@param?joinPoint?join?point?for?advice
          ?????*/
          ????@Around("detailPointcut()")
          ????public?Object?detailAround(ProceedingJoinPoint?joinPoint)?throws?Throwable?{
          ????????Object?result;
          ????????currentTime.set(System.currentTimeMillis());??????//保存當(dāng)前時(shí)間
          ????????result?=?joinPoint.proceed();?????????????????????//獲得函數(shù)運(yùn)行結(jié)果

          ????????//操作時(shí)長(zhǎng)
          ????????Long?time=System.currentTimeMillis()?-?currentTime.get();
          ????????System.out.println("操作時(shí)長(zhǎng):"+time?+"?毫秒"?);

          ????????HttpServletRequest?request?=?RequestHolder.getHttpServletRequest();??
          ????????//獲取ip值
          ????????String?ip=request.getRemoteAddr();
          ????????System.out.println("ip:"+?ip?);
          ????????//獲得瀏覽器類型
          ????????String?browser=?StringUtils.getBrowser(request);????//getBrowser()方法見(jiàn)下面
          ????????System.out.println("瀏覽器類型:"+?browser?);
          ????????//獲取操作時(shí)間
          ????????Time?t?=new?Time(System.currentTimeMillis());
          ????????LocalTime?localTime?=?t.toLocalTime();
          ????????String?st?=?localTime.toString();??????????????????//獲取當(dāng)前時(shí)間(毫秒)轉(zhuǎn)化為標(biāo)準(zhǔn)時(shí)間顯示
          ????????System.out.println("當(dāng)前操作時(shí)間:"+?st?);???????????

          ????????//獲取操作人
          ????????String?currentUsername?=?SecurityUtils.getCurrentUsername();????//獲取系統(tǒng)登錄用戶,從cookie或reids中取
          ????????System.out.println("當(dāng)前操作人:"+?currentUsername?);
          ????????//獲取方法名
          ????????MethodSignature?signature?=?(MethodSignature)?joinPoint.getSignature();????
          ????????String?methodName?=?joinPoint.getTarget().getClass().getName()?+?"."?+?signature.getName()?+?"()";
          ????????System.out.println("當(dāng)前具體操作方法:"+?methodName?);
          ????????//獲取描述
          ????????Method?method?=?signature.getMethod();
          ????????DetailRequest?annotation?=?method.getAnnotation(DetailRequest.class);
          ????????String?value?=?annotation.value();
          ????????System.out.println("當(dāng)前具體操作注解名:"+?value?);?????????//獲取注解中傳遞的參數(shù)
          ????????return?result;
          ????}
          }

          其中所用到的工具類RequestHolder,其定義為

          public?class?RequestHolder?{

          ????public?static?HttpServletRequest?getHttpServletRequest()?{
          ????????return?((ServletRequestAttributes)Objects.
          ???????????requireNonNull(RequestContextHolder.
          ???????????getRequestAttributes())).getRequest();
          ????}
          }

          StringUtils.getBrowser()方法定義如下:

          ????public?static?String?getBrowser(HttpServletRequest?request)?{
          ????????UserAgent?userAgent?=?UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
          ????????Browser?browser?=?userAgent.getBrowser();
          ????????return?browser.getName();
          ????}

          2.3 自定義注解的使用

          該函數(shù)功能為查詢指定表格的所有數(shù)據(jù)

          ????@GetMapping
          ????@DetailRequest("自定義測(cè)試")????//傳遞"自定義測(cè)試"字符串
          ????public?ResponseEntity?query(MyJobQueryCriteria?criteria,Pageable?pageable){
          ????????return?new?ResponseEntity<>(myJobService.queryAll(criteria,pageable),HttpStatus.OK);
          ????}

          2.4 啟動(dòng)類上添加注解

          @EnableAspectJAutoProxy

          2.5 觀察結(jié)果

          系統(tǒng)啟動(dòng)時(shí)(使用的是eladmin開(kāi)源框架,具體使用見(jiàn)我的博客):

          可以看到已經(jīng)成功加載自定義注解
          接下來(lái),進(jìn)入指定頁(yè)面,加載public ResponseEntity query(MyJobQueryCriteria criteria,Pageable pageable),可以看到輸出結(jié)果:



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

          本文鏈接:

          http://blog.csdn.net/ws6afa88/article/details/108590248




          粉絲福利:108本java從入門(mén)到大神精選電子書(shū)領(lǐng)取

          ???

          ?長(zhǎng)按上方鋒哥微信二維碼?2 秒
          備注「1234」即可獲取資料以及
          可以進(jìn)入java1234官方微信群



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

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

          手機(jī)掃一掃分享

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

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                    <th id="afajh"><progress id="afajh"></progress></th>
                    欧美日韩在线视频播放 | 黄片欧美在线观看 | 亚洲欧视频在线播放 | 9999re | 精品交换一区二区三区无码 |