一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - SpringBoot AOP使用筆記

SpringBoot AOP使用筆記

2021-07-13 15:41裴星宙 Java教程

今天小編就為大家分享一篇關于SpringBoot AOP使用筆記,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

1. 啟用aop

a. 在類上添加@aspect注解

b. 注入該類, 可以使用@component進行注入到spring容器中

2. 通過pointcut對象創建切入點

a. 在某個方法使用類似下面的方法進行注入

?
1
2
3
@pointcut("execution(* com.sguess.service.iaopservice.*(..))")
  private void pointcut() {
  }

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

?
1
2
3
@pointcut("execution(* com.sguess.service.iaopservice.fun1(..))")
    private void pointcut2() {
    }

這個的方法名就位pointcut2, 方法名不一樣.  

b. 創建after方法,before方法

?
1
2
3
4
@after(value = "pointcut()")
  public void doafter() {
    system.out.println("do aop after function 01");
  }

i. after方法是指, 在配置了的切入點被執行后, 執行該方法. 
ii. value中的pointcut() 是我們前面在創建@pointcut中的方法名. 也就是說,是通過方法名和切入點進行匹配的. 
iii. 這個的方法名可以隨便起. 
iv. before方法同理

c. 帶return的after方法,

?
1
2
3
4
@afterreturning(returning = "str", pointcut = "pointcut()")
  public void doafterreturning(string str) throws exception {
    system.out.println("return value is: " + str);
  }

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

3. 通過注解, 使用切入點

a. 監聽方法參數

?
1
2
3
4
5
6
7
8
9
10
11
12
@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. 執行順序:

a.around的方法優先于before/after執行,after優先于afterreturn. 

i. 代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@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;
      }
    }

執行結果:

start aroundfunction
around process start
before function
around process end
after around process
after function
afterreturn function

5.小結:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@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;
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

原文鏈接:https://blog.csdn.net/sanpic/article/details/82800017

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人福利网 | 丝袜足液精子免费视频 | 暖暖视频高清图片免费完整版 | 狠狠色成人综合 | 国产一级精品高清一级毛片 | 91大片淫黄大片在线天堂 | 午夜精品久久久久 | 无码乱人伦一区二区亚洲一 | 日本生活中的玛丽 | 国内精品久久久久影院中国 | 日韩无砖2021特黄 | 春意影院午夜爽爽爽免费 | 韩国女主播在线大尺无遮挡 | 色婷婷综合久久久 | 毛片区| 亚洲AV无码乱码在线观看浪潮 | 成人国产网站v片免费观看 成人国产精品视频 | 亚洲国产精品久久精品怡红院 | 暗卫调教女主肉高h | free chinese麻豆| 好大水好多好爽好硬好深视频 | 日韩高清成人毛片不卡 | 96免费精品视频在线 | 亚洲另类激情 | 高h辣h双处全是肉军婚 | 91夜夜人人揉人人捏人人添 | 男女交性特一级 | 99精品视频一区在线观看miya | 涩涩屋在线播放 | 成人在线观看一区 | 亚洲成人福利 | 午夜欧美精品久久久久久久久 | 91庥豆果冻天美精东蜜桃传媒 | 天堂网在线.www天堂在线资源 | 视频免费观看在线播放高清 | 亚洲AV无码偷拍在线观看 | 四虎影视网址 | 色婷婷激婷婷深爱五月老司机 | 毛片在线免费观看网站 | 欧洲一级黑寡妇 | 蜜柚精彩在线观看 |