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

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

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

服務器之家 - 編程語言 - Java教程 - 詳解使用Spring AOP和自定義注解進行參數檢查

詳解使用Spring AOP和自定義注解進行參數檢查

2021-04-22 10:59liaosilzu2007 Java教程

本篇文章主要介紹了詳解使用Spring AOP和自定義注解進行參數檢查,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

引言

使用springmvc作為controller層進行web開發時,經常會需要對controller中的方法進行參數檢查。本來springmvc自帶@valid和@validated兩個注解可用來檢查參數,但只能檢查參數是bean的情況,對于參數是string或者long類型的就不適用了,而且有時候這兩個注解又突然失效了(沒有仔細去調查過原因),對此,可以利用spring的aop和自定義注解,自己寫一個參數校驗的功能。

代碼示例

注意:本節代碼只是一個演示,給出一個可行的思路,并非完整的解決方案。

本項目是一個簡單web項目,使用到了:spring、springmvc、maven、jdk1.8

項目結構:

詳解使用Spring AOP和自定義注解進行參數檢查

自定義注解:

validparam.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.lzumetal.ssm.paramcheck.annotation;
 
import java.lang.annotation.*;
 
/**
 * 標注在參數bean上,表示需要對該參數校驗
 */
@target({elementtype.parameter})
@retention(retentionpolicy.runtime)
@documented
public @interface validparam {
  
  
}

notnull.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.lzumetal.ssm.paramcheck.annotation;
 
import java.lang.annotation.*;
 
@target({elementtype.field, elementtype.parameter})
@retention(retentionpolicy.runtime)
@documented
public @interface notnull {
 
  string msg() default "字段不能為空";
  
}

notempty.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
package com.lzumetal.ssm.paramcheck.annotation;
 
import java.lang.annotation.*;
 
@target({elementtype.field, elementtype.parameter})
@retention(retentionpolicy.runtime)
@documented
public @interface notempty {
 
  string msg() default "字段不能為空";
  
}

切面類

paramcheckaspect.java:

?
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.lzumetal.ssm.paramcheck.aspect;
import com.lzumetal.ssm.paramcheck.annotation.notempty;
import com.lzumetal.ssm.paramcheck.annotation.notnull;
import com.lzumetal.ssm.paramcheck.annotation.validparam;
import org.apache.commons.lang3.stringutils;
import org.aspectj.lang.joinpoint;
import org.aspectj.lang.annotation.aspect;
import org.aspectj.lang.annotation.before;
import org.aspectj.lang.reflect.methodsignature;
import org.springframework.stereotype.component;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
import java.lang.reflect.field;
import java.lang.reflect.parameter;
import java.util.arrays;
/**
 * 參數檢查切面類
 */
@aspect
@component
public class paramcheckaspect {
 
  @before("execution(* com.lzumetal.ssm.paramcheck.controller.*.*(..))")
  public void paramcheck(joinpoint joinpoint) throws exception {
    //獲取參數對象
    object[] args = joinpoint.getargs();
    //獲取方法參數
    methodsignature signature = (methodsignature) joinpoint.getsignature();
    parameter[] parameters = signature.getmethod().getparameters();
    for (int i = 0; i < parameters.length; i++) {
      parameter parameter = parameters[i];
      //java自帶基本類型的參數(例如integer、string)的處理方式
      if (isprimite(parameter.gettype())) {
        notnull notnull = parameter.getannotation(notnull.class);
        if (notnull != null && args[i] == null) {
          throw new runtimeexception(parameter.tostring() + notnull.msg());
        }
        //todo
        continue;
      }
      /*
       * 沒有標注@validparam注解,或者是httpservletrequest、httpservletresponse、httpsession時,都不做處理
      */
      if (parameter.gettype().isassignablefrom(httpservletrequest.class) || parameter.gettype().isassignablefrom(httpsession.class) ||
          parameter.gettype().isassignablefrom(httpservletresponse.class) || parameter.getannotation(validparam.class) == null) {
        continue;
      }
      class<?> paramclazz = parameter.gettype();
      //獲取類型所對應的參數對象,實際項目中controller中的接口不會傳兩個相同的自定義類型的參數,所以此處直接使用findfirst()
      object arg = arrays.stream(args).filter(ar -> paramclazz.isassignablefrom(ar.getclass())).findfirst().get();
      //得到參數的所有成員變量
      field[] declaredfields = paramclazz.getdeclaredfields();
      for (field field : declaredfields) {
        field.setaccessible(true);
        //校驗標有@notnull注解的字段
        notnull notnull = field.getannotation(notnull.class);
        if (notnull != null) {
          object fieldvalue = field.get(arg);
          if (fieldvalue == null) {
            throw new runtimeexception(field.getname() + notnull.msg());
          }
        }
        //校驗標有@notempty注解的字段,notempty只用在string類型上
        notempty notempty = field.getannotation(notempty.class);
        if (notempty != null) {
          if (!string.class.isassignablefrom(field.gettype())) {
            throw new runtimeexception("notempty annotation using in a wrong field class");
          }
          string fieldstr = (string) field.get(arg);
          if (stringutils.isblank(fieldstr)) {
            throw new runtimeexception(field.getname() + notempty.msg());
          }
        }
      }
    }
  }
  /**
   * 判斷是否為基本類型:包括string
   * @param clazz clazz
   * @return true:是;   false:不是
   */
  private boolean isprimite(class<?> clazz){
    return clazz.isprimitive() || clazz == string.class;
  }
}

參數javabean

studentparam.java:

?
1
2
3
4
5
6
7
8
9
10
11
package com.lzumetal.ssm.paramcheck.requestparam;
import com.lzumetal.ssm.paramcheck.annotation.notempty;
import com.lzumetal.ssm.paramcheck.annotation.notnull;
public class studentparam {
  @notnull
  private integer id;
  private integer age;
  @notempty
  private string name;
  //get、set方法省略...
}

驗證參數校驗的controller

testcontroller.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.lzumetal.ssm.paramcheck.controller;
import com.google.gson.gson;
import com.lzumetal.ssm.paramcheck.annotation.notnull;
import com.lzumetal.ssm.paramcheck.annotation.validparam;
import com.lzumetal.ssm.paramcheck.requestparam.studentparam;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import org.springframework.web.bind.annotation.responsebody;
@controller
public class testcontroller {
  private static gson gson = new gson();
  @responsebody
  @requestmapping(value = "/test", method = requestmethod.post)
  public studentparam checkparam(@validparam studentparam param, @notnull integer limit) {
    system.out.println(gson.tojson(param));
    system.out.println(limit);
    return param;
  }
}

本節示例代碼已上傳至github:https://github.com/liaosilzu2007/ssm-parent.git

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000014454607

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 擦逼视频 | 成年美女黄网色大观看全 | 精品午夜久久网成年网 | 男人女人日皮 | 96日本xxxxxxxxx70| 性一交一乱一伧老太 | 日本又大又硬又粗的视频 | 亚洲国产99在线精品一区二区 | 99久久6er热免费精品 | 姐姐不~不可以动漫在线观看 | 久久99热在线观看7 久久99精品涩AV毛片观看 | 77色视频在线 | 8天堂资源在线官网 | 天天做天天爰夜夜爽 | jj视频免费看 | 亚洲高清在线天堂精品 | 狠狠色伊人亚洲综合网站色 | 成人亚洲欧美日韩中文字幕 | 韩国伊人 | 日韩人成免费网站大片 | 5x社区在线观看直接进入 | 俄罗斯男男激情1069gay | 亚洲123区| 国产福利兔女郎在线观看 | 欧美日韩一区二区三区韩大 | 91香蕉视频网址 | 欧美男同互吃gay老头 | 日本大片在线 | 国产精品视频一区二区三区经 | 国产精品99久久免费观看 | 美女伊人网 | 四虎影院网址大全 | 99re热精品这里精品 | 91精品大神国产在线播放 | 91寡妇天天综合久久影院 | 床戏小说 | 操弄哥哥的108种姿势 | 超级乱淫寡妇 | 精品久久99麻豆蜜桃666 | avtt一区| 俄罗斯女同和女同xx |