国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

代碼示例

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

本項目是一個簡單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;
  }
}

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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区二区三区成人 | 亚洲天堂中文字幕 | 欧美成人一区二区 | 一区二区三区国产好的精 | 日韩免费在线观看 | 日韩欧美精品在线 | 91丁香婷婷综合久久欧美 | 精品久久中文 | 激情免费视频 | 热久久影院 | 国产精品久久久久久久久久久久久 | 小川阿佐美88av在线播放 | 精品亚洲一区二区 | 国产精品国产成人国产三级 | 99综合在线 | 一区二区三区成人久久爱 | 久久成人av | 爱干视频 | 久色视频在线观看 | 亚洲天堂一区 | 日韩国产欧美视频 | 天天澡天天狠天天天做 | 欧美日韩一级二级三级 | av中文字幕在线 | 欧美日韩一级二级三级 | 99精品国产高清一区二区麻豆 | 欧美一区第一页 | 久久久久久毛片免费播放 | 天堂一区 | 91 久久| 中文字幕一区二区三区乱码在线 | 精品国产乱码久久久久久1区2区 | 一区二区三区免费在线观看 | www中文在线观看 | 国产精品久久久久久久久免费 | 精品久久久久久久久久久久久久 | www.se天堂 | 欧美片网站免费 | 久久久久久久国产精品 | 国产精品一区在线观看 | 国产精品福利在线观看 |