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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成年私人影院免费视频网站 | 青丝视频免费版在线看 | 好男人资源免费播放 | 亚洲色欧美图 | 关晓彤被调教出奶水的视频 | 天堂成人在线视频 | 国产麻豆剧果冻传媒影视4934 | 日韩欧美一区二区不卡 | 欧美 国产 日韩 第一页 | 精品免费 | 国产精自产拍久久久久久 | 啪啪无尽3d动漫漫画免费网站 | 我被黄总征服的全过程 | poren18美女 | 精品人人视屏 | 幻女free性俄罗斯第一次摘花 | 国产在线综合网 | 亚洲精品视频在线免费 | 亚洲成人一区 | 大团圆6全文在线阅读 | 高清麻生希在线 | 国产品精人成福利视频 | 亚洲国产99在线精品一区二区 | 国产亚洲sss在线观看 | 青青草99热久久 | 激情亚洲 | 第一次破学生处破 | 精品国产区一区二区三区在线观看 | 精品久久久久久久久久香蕉 | 91污污视频 | 欧洲兽皇 | 美女校花被调教出奶水 | 久久久精品国产免费A片胖妇女 | 亚洲免费精品 | 视频一区二区三区在线观看 | 丝瓜视频黄瓜视频 | 午夜香蕉| 99久久一香蕉国产线看观看 | 精品国产日韩亚洲一区在线 | 日韩精品视频在线播放 | 欧美折磨另类系列sm |