欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解使用Spring AOP和自定義注解進(jìn)行參數(shù)檢查

 更新時(shí)間:2018年04月18日 16:53:51   作者:liaosilzu2007  
本篇文章主要介紹了詳解使用Spring AOP和自定義注解進(jìn)行參數(shù)檢查,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

引言

使用SpringMVC作為Controller層進(jìn)行Web開發(fā)時(shí),經(jīng)常會(huì)需要對(duì)Controller中的方法進(jìn)行參數(shù)檢查。本來SpringMVC自帶@Valid和@Validated兩個(gè)注解可用來檢查參數(shù),但只能檢查參數(shù)是bean的情況,對(duì)于參數(shù)是String或者Long類型的就不適用了,而且有時(shí)候這兩個(gè)注解又突然失效了(沒有仔細(xì)去調(diào)查過原因),對(duì)此,可以利用Spring的AOP和自定義注解,自己寫一個(gè)參數(shù)校驗(yàn)的功能。

代碼示例

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

本項(xiàng)目是一個(gè)簡(jiǎn)單Web項(xiàng)目,使用到了:Spring、SpringMVC、Maven、JDK1.8

項(xiàng)目結(jié)構(gòu):

自定義注解:

ValidParam.java:

package com.lzumetal.ssm.paramcheck.annotation;

import java.lang.annotation.*;

/**
 * 標(biāo)注在參數(shù)bean上,表示需要對(duì)該參數(shù)校驗(yàn)
 */
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ValidParam {
  
  
}

NotNull.java:

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:

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:

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;
/**
 * 參數(shù)檢查切面類
 */
@Aspect
@Component
public class ParamCheckAspect {

  @Before("execution(* com.lzumetal.ssm.paramcheck.controller.*.*(..))")
  public void paramCheck(JoinPoint joinPoint) throws Exception {
    //獲取參數(shù)對(duì)象
    Object[] args = joinPoint.getArgs();
    //獲取方法參數(shù)
    MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Parameter[] parameters = signature.getMethod().getParameters();
    for (int i = 0; i < parameters.length; i++) {
      Parameter parameter = parameters[i];
      //Java自帶基本類型的參數(shù)(例如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;
      }
      /*
       * 沒有標(biāo)注@ValidParam注解,或者是HttpServletRequest、HttpServletResponse、HttpSession時(shí),都不做處理
      */
      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();
      //獲取類型所對(duì)應(yīng)的參數(shù)對(duì)象,實(shí)際項(xiàng)目中Controller中的接口不會(huì)傳兩個(gè)相同的自定義類型的參數(shù),所以此處直接使用findFirst()
      Object arg = Arrays.stream(args).filter(ar -> paramClazz.isAssignableFrom(ar.getClass())).findFirst().get();
      //得到參數(shù)的所有成員變量
      Field[] declaredFields = paramClazz.getDeclaredFields();
      for (Field field : declaredFields) {
        field.setAccessible(true);
        //校驗(yàn)標(biāo)有@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());
          }
        }
        //校驗(yàn)標(biāo)有@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;
  }
}

參數(shù)JavaBean

StudentParam.java:

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方法省略...
}

驗(yàn)證參數(shù)校驗(yàn)的Controller

TestController.java:

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java通過url下載文件并輸出的方法

    java通過url下載文件并輸出的方法

    下面小編就為大家分享一篇java通過url下載文件并輸出的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Java在創(chuàng)建文件時(shí)指定編碼的實(shí)現(xiàn)方法

    Java在創(chuàng)建文件時(shí)指定編碼的實(shí)現(xiàn)方法

    本文主要介紹了Java在創(chuàng)建文件時(shí)指定編碼的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 利用java實(shí)現(xiàn)二叉搜索樹

    利用java實(shí)現(xiàn)二叉搜索樹

    這篇文章主要介紹了利用java實(shí)現(xiàn)二叉搜索樹,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java死鎖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java死鎖_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    死鎖是兩個(gè)甚至多個(gè)線程被永久阻塞時(shí)的一種運(yùn)行局面,這種局面的生成伴隨著至少兩個(gè)線程和兩個(gè)或者多個(gè)資源。在這里我已寫好一個(gè)簡(jiǎn)單的程序,它將會(huì)引起死鎖方案然后我們就會(huì)明白如何分析它
    2017-06-06
  • mybatis中foreach嵌套if標(biāo)簽方式

    mybatis中foreach嵌套if標(biāo)簽方式

    這篇文章主要介紹了mybatis中foreach嵌套if標(biāo)簽方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring-AOP自動(dòng)創(chuàng)建代理之BeanNameAutoProxyCreator實(shí)例

    Spring-AOP自動(dòng)創(chuàng)建代理之BeanNameAutoProxyCreator實(shí)例

    這篇文章主要介紹了Spring-AOP自動(dòng)創(chuàng)建代理之BeanNameAutoProxyCreator實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 你知道將Bean交給Spring容器管理有幾種方式(推薦)

    你知道將Bean交給Spring容器管理有幾種方式(推薦)

    Spring核心是?IOC?和?AOP?,我們?cè)赟pring項(xiàng)目中,我們需要將Bean交給Spring容器,也就是IOC管理,這樣你才可以使用注解來進(jìn)行依賴注入,這篇文章主要介紹了你知道將Bean交給Spring容器管理有幾種方式,需要的朋友可以參考下
    2022-10-10
  • BeanUtils.copyProperties()屬性名相同但是類型不同問題

    BeanUtils.copyProperties()屬性名相同但是類型不同問題

    這篇文章主要介紹了BeanUtils.copyProperties()屬性名相同但是類型不同問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Spring中MVC模塊代碼詳解

    Spring中MVC模塊代碼詳解

    這篇文章主要介紹了Spring中MVC模塊代碼詳解,涉及Controller的簡(jiǎn)單介紹,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-11-11
  • SpringMVC中的HandlerMapping詳解

    SpringMVC中的HandlerMapping詳解

    這篇文章主要介紹了SpringMVC中的HandlerMapping詳解,HandlerMapping是請(qǐng)求映射處理器,也就是通過請(qǐng)求的url找到對(duì)應(yīng)的邏輯處理單元(Controller),注意這里只是建立請(qǐng)求與Controller的映射關(guān)系,最終的處理是通過HandlerAdapt來進(jìn)行處理的,需要的朋友可以參考下
    2023-09-09

最新評(píng)論