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

SpringBoot AOP使用筆記

 更新時(shí)間:2019年01月30日 11:31:40   作者:裴星宙  
今天小編就為大家分享一篇關(guān)于SpringBoot AOP使用筆記,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧

1. 啟用AOP

a. 在類(lèi)上添加@Aspect注解

b. 注入該類(lèi), 可以使用@Component進(jìn)行注入到Spring容器中

2. 通過(guò)PointCut對(duì)象創(chuàng)建切入點(diǎn)

a. 在某個(gè)方法使用類(lèi)似下面的方法進(jìn)行注入

@Pointcut("execution(* com.sguess.service.IAOPService.*(..))")
  private void pointcut() {
  }

i. 其中,execution表達(dá)式為
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)  
ii. 注意, pointcut()方法名是后面切入的時(shí)候需要使用的
iii. 方法內(nèi)可以什么也不寫(xiě), 寫(xiě)了也調(diào)不到
iv. 也可以創(chuàng)建多個(gè)PointCut,例如再創(chuàng)建一個(gè)

@Pointcut("execution(* com.sguess.service.IAOPService.fun1(..))")
    private void pointcut2() {
    }

這個(gè)的方法名就位pointcut2, 方法名不一樣.  

b. 創(chuàng)建After方法,Before方法

@After(value = "pointcut()")
  public void doAfter() {
    System.out.println("Do AOP After function 01");
  }

i. After方法是指, 在配置了的切入點(diǎn)被執(zhí)行后, 執(zhí)行該方法. 
ii. value中的pointcut() 是我們前面在創(chuàng)建@Pointcut中的方法名. 也就是說(shuō),是通過(guò)方法名和切入點(diǎn)進(jìn)行匹配的. 
iii. 這個(gè)的方法名可以隨便起. 
iv. Before方法同理

c. 帶Return的After方法,

@AfterReturning(returning = "str", pointcut = "pointcut()")
  public void doAfterReturning(String str) throws Exception {
    System.out.println("Return value is: " + str);
  }

i. AfterReturn是指在被切入的方法執(zhí)行后, 獲取其返回值, 再執(zhí)行該方法. 注意關(guān)鍵, 這個(gè)可以進(jìn)行操作返回值. 
ii. returning = "str",是指, 假設(shè)切入方法的返回的值變量名為str
doAfterReturning(String str)方法的參數(shù)變量名必須和和returning保持一致, 這里也叫作str. 然后才能在方法體中使用.
iii. pointcut = "pointcut()"同樣是指前面聲明的pointcut方法名

3. 通過(guò)注解, 使用切入點(diǎn)

a. 監(jiān)聽(tīng)方法參數(shù)

@Before("execution(public int com.sguess.service.*(int, int))")
  public void beforMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args);
  }
  @After("execution(public int com.sguess.service.*(int, int))")
  public void afterMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args);
  }

4. 執(zhí)行順序:

a.Around的方法優(yōu)先于Before/After執(zhí)行,After優(yōu)先于AfterReturn. 

i. 代碼

@Before("execution(public int com.sguess.service.*.*(int, int))")
      public void beforMethod(JoinPoint point) {
        System.out.println("Before function");
      }
      @After("execution(public int com.sguess.service.*.*(int, int))")
      public void afterMethod(JoinPoint point) {
        System.out.println("After function");
      }
      @AfterReturning("execution(public int com.sguess.service.*.*(int, int))")
      public void afterReturnMethod(JoinPoint point) {
        System.out.println("AfterReturn function");
      }
      @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e")
      public void afterReturningThrowing(JoinPoint point, Exception e) {
        System.out.println("AfterReturnThrowing function");
      }
      @Around("execution(public int com.sguess.service.*.*(int, int))")
      public Object aroundMethod(ProceedingJoinPoint pdj) {
        System.out.println("Start AroundFunction");
        Object result = null;
        try {
          System.out.println("Around process start");
          result = pdj.proceed();
          System.out.println("Around process end");
        } catch (Throwable e) {
          System.out.println("Around process exception");
        }
        System.out.println("After Around process");
        return result;
      }
    }

執(zhí)行結(jié)果:

Start AroundFunction
Around process start
Before function
Around process end
After Around process
After function
AfterReturn function

5.小結(jié):

  @AfterReturning(returning = "str", pointcut = "pointcut()")
  public void doAfterReturning(String str) throws Exception {
    System.out.println("Return value is: " + str);
  }
  @Before("execution(public int com.sguess.service.*.*(int, int))")
  public void beforMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args);
  }
  @After("execution(public int com.sguess.service.*.*(int, int))")
  public void afterMethod(JoinPoint point) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args);
  }
  @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e")
  public void afterReturningThrowing(JoinPoint point, Exception e) {
    String methodName = point.getSignature().getName();
    List<Object> args = Arrays.asList(point.getArgs());
    System.out.println("AfterReturningThrowing FunctionName:" + methodName + ",ParameterName:" + args + ",Exception:" + e);
  }
  @Around("execution(public int com.sguess.service.*.*(int, int))")
  public Object aroundMethod(ProceedingJoinPoint pdj) {
      System.out.println("Start AroundFunction");
      Object result = null;
      try {
          System.out.println("Around process start");
          result = pdj.proceed();
          System.out.println("Around process end");
      } catch (Throwable e) {
          System.out.println("Around process exception");
      }
      System.out.println("After Around process");
      return result;
  }

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

  • gradle安裝配置以及使用最新方法

    gradle安裝配置以及使用最新方法

    這篇文章主要給大家介紹了關(guān)于gradle安裝配置以及使用的相關(guān)資料,Gradle是一個(gè)基于ApacheAnt和ApacheMaven概念的項(xiàng)目自動(dòng)化構(gòu)建開(kāi)源工具,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • SpringBoot數(shù)據(jù)訪問(wèn)的實(shí)現(xiàn)

    SpringBoot數(shù)據(jù)訪問(wèn)的實(shí)現(xiàn)

    本文主要介紹了SpringBoot數(shù)據(jù)訪問(wèn)的實(shí)現(xiàn),引入各種xxxTemplate,xxxRepository來(lái)簡(jiǎn)化我們對(duì)數(shù)據(jù)訪問(wèn)層的操作,感興趣的可以了解一下
    2023-11-11
  • ProtoStuff不支持BigDecimal序列化及反序列化詳解

    ProtoStuff不支持BigDecimal序列化及反序列化詳解

    這篇文章主要為大家介紹了ProtoStuff不支持BigDecimal序列化/反序列化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 解決IDEA Gradle構(gòu)建報(bào)錯(cuò)''Cause: zip END header not found''

    解決IDEA Gradle構(gòu)建報(bào)錯(cuò)''Cause: zip END header not found''

    這篇文章主要介紹了解決IDEA Gradle構(gòu)建報(bào)錯(cuò)"Cause: zip END header not found"的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • JAVA中JVM的重排序詳細(xì)介紹

    JAVA中JVM的重排序詳細(xì)介紹

    重排序通常是編譯器或運(yùn)行時(shí)環(huán)境為了優(yōu)化程序性能而采取的對(duì)指令進(jìn)行重新排序執(zhí)行的一種手段。重排序分為兩類(lèi):編譯期重排序和運(yùn)行期重排序,分別對(duì)應(yīng)編譯時(shí)和運(yùn)行時(shí)環(huán)境
    2014-05-05
  • 關(guān)于Mybatis動(dòng)態(tài)sql中test的坑點(diǎn)總結(jié)

    關(guān)于Mybatis動(dòng)態(tài)sql中test的坑點(diǎn)總結(jié)

    這篇文章主要介紹了關(guān)于Mybatis動(dòng)態(tài)sql中test的坑點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • idea打不開(kāi)項(xiàng)目問(wèn)題的解決過(guò)程(典型案例)

    idea打不開(kāi)項(xiàng)目問(wèn)題的解決過(guò)程(典型案例)

    idea導(dǎo)入項(xiàng)目,起環(huán)境的時(shí)候經(jīng)常會(huì)碰到項(xiàng)目環(huán)境起不來(lái)的情況,下面這篇文章主要介紹了idea打不開(kāi)項(xiàng)目問(wèn)題的解決過(guò)程,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • 關(guān)于kafka-consumer-offset位移問(wèn)題

    關(guān)于kafka-consumer-offset位移問(wèn)題

    這篇文章主要介紹了關(guān)于kafka-consumer-offset位移問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL

    Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL

    本文主要介紹了Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 數(shù)據(jù)同步利器DataX簡(jiǎn)介及如何使用

    數(shù)據(jù)同步利器DataX簡(jiǎn)介及如何使用

    DataX?是阿里云?DataWorks數(shù)據(jù)集成?的開(kāi)源版本,使用Java?語(yǔ)言編寫(xiě),在阿里巴巴集團(tuán)內(nèi)被廣泛使用的離線數(shù)據(jù)同步工具/平臺(tái),今天給大家分享一個(gè)阿里開(kāi)源的數(shù)據(jù)同步工具DataX,在Github擁有14.8k的star,非常受歡迎
    2024-02-02

最新評(píng)論

和政县| 中江县| 祁东县| 南江县| 慈溪市| 柘荣县| 施秉县| 贵港市| 莫力| 柏乡县| 崇仁县| 沐川县| 安平县| 江城| 贵德县| 大关县| 邹平县| 竹溪县| 龙泉市| 宣武区| 梨树县| 阿克陶县| 巴彦淖尔市| 苗栗市| 广南县| 蒙城县| 和田县| 镇远县| 闵行区| 元阳县| 南靖县| 南川市| 葵青区| 周口市| 色达县| 嘉鱼县| 四子王旗| 金川县| 双辽市| 顺义区| 车险|