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

一文搞懂SpringMVC中@InitBinder注解的使用

 更新時間:2022年06月29日 14:49:00   作者:Spring全家桶實戰(zhàn)案例  
@InitBinder方法可以注冊控制器特定的java.bean.PropertyEditor或Spring Converter和 Formatter組件。本文通過示例為大家詳細講講@InitBinder注解的使用,需要的可以參考一下

環(huán)境:Springboot2.4.12

簡介

?@Controller或@ControllerAdvice類可以有@InitBinder方法來初始化WebDataBinder的實例,這些方法可以:

  • 將請求參數(shù)(即表單或查詢數(shù)據(jù))綁定到模型對象。
  • 將基于字符串的請求值(如請求參數(shù)、路徑變量、頭、cookie等)轉(zhuǎn)換為控制器方法參數(shù)的目標類型。
  • 渲染HTML表單時,將模型對象的值格式化為字符串值。

@InitBinder方法可以注冊控制器特定的java.bean.PropertyEditor或Spring Converter和 Formatter組件。另外,你可以使用MVC配置在全局共享的FormattingConversionService中注冊Converter和Formatter類型。

@InitBinder方法支持許多與@RequestMapping方法相同的參數(shù),除了@ModelAttribute(命令對象)參數(shù)。通常,它們是用WebDataBinder參數(shù)(用于注冊)和一個void返回值聲明的。

應(yīng)用示例

@RestController
@RequestMapping("/demos")
public class DemoController {
  @InitBinder // 1
  public void bind(WebDataBinder binder) { // 2
    binder.registerCustomEditor(Long.class, new PropertyEditorSupport() { // 3
      @Override
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(Long.valueOf(text) + 666L) ;
      }
    }) ;
  }
  @GetMapping("/index")
  public Object index(Long id) {
    return "index - " + id ;
  }
}

注意以下幾點:

  • 使用 @InitBinder 注解。
  • 接收 WebDataBinder 參數(shù)。
  • 注冊自定義的轉(zhuǎn)換器。
  • 方法返回值必須是 void。

在上面的示例中注冊了一個類型轉(zhuǎn)換器從字符串轉(zhuǎn)換為Long類型 并且在原來值基礎(chǔ)上增加了666L。

原理解讀

HandlerAdapter 執(zhí)行。

public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter implements BeanFactoryAware, InitializingBean {
  protected ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
    // ...
    // 這里會查找當前執(zhí)行的Controller中定義的所有@InitBinder注解的方法
    WebDataBinderFactory binderFactory = getDataBinderFactory(handlerMethod);
    ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(handlerMethod);
    invocableMethod.invokeAndHandle(webRequest, mavContainer);
    // ...
  }
}

ServletInvocableHandlerMethod 執(zhí)行。

public class ServletInvocableHandlerMethod extends InvocableHandlerMethod {
  public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
    // 調(diào)用父類方法
    Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
    // ...
  }
}
// 執(zhí)行父類方法調(diào)用
public class InvocableHandlerMethod extends HandlerMethod {
  public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
    Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
    return doInvoke(args);
  }
  protected Object[] getMethodArgumentValues(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
    Object[] args = new Object[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
      // 解析參數(shù)
      args[i] = this.resolvers.resolveArgument(parameter, mavContainer, request, this.dataBinderFactory);
    }
  }
}

參數(shù)解析

在上面的Controller示例中,參數(shù)的解析器是RequestParamMethodArgumentResolver。

調(diào)用父類的resolveArgument方法。

public abstract class AbstractNamedValueMethodArgumentResolver {
  public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
    // 封裝方法參數(shù)的名稱這里為:id
    NamedValueInfo namedValueInfo = getNamedValueInfo(parameter);
    // resolvedName = id
    Object resolvedName = resolveEmbeddedValuesAndExpressions(namedValueInfo.name);
    // ...
    // 獲取參數(shù)名對應(yīng)的請求參數(shù)值:/demos/index?id=100 , 這就返回100
    Object arg = resolveName(resolvedName.toString(), nestedParameter, webRequest);
    // ...
    if (binderFactory != null) {
      // 根據(jù)當前的Request對象及請求參數(shù)名創(chuàng)建WebDataBinder對象
      // 內(nèi)部創(chuàng)建的ExtendedServletRequestDataBinder對象
      WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
      try {
        // 執(zhí)行類型轉(zhuǎn)換
        arg = binder.convertIfNecessary(arg, parameter.getParameterType(), parameter);
      }
    }
  }
}
// 創(chuàng)建WebDataBinder對象
public class DefaultDataBinderFactory implements WebDataBinderFactory {
  public final WebDataBinder createBinder(NativeWebRequest webRequest, @Nullable Object target, String objectName) throws Exception {
    WebDataBinder dataBinder = createBinderInstance(target, objectName, webRequest);
    if (this.initializer != null) {
      // 初始化WebDataBinder對象,這里最主要的就是為其設(shè)置類型轉(zhuǎn)換器
      this.initializer.initBinder(dataBinder, webRequest);
    }
    // 初始化執(zhí)行@InitBinder注解的方法
    initBinder(dataBinder, webRequest);
    return dataBinder;
  }
}
public class InitBinderDataBinderFactory extends DefaultDataBinderFactory {
  public void initBinder(WebDataBinder dataBinder, NativeWebRequest request) throws Exception {
    // 遍歷所有@InitBinder注解的方法
    for (InvocableHandlerMethod binderMethod : this.binderMethods) {
      if (isBinderMethodApplicable(binderMethod, dataBinder)) {
        // 這里就是執(zhí)行@InitBinder注解的方法
       Object returnValue = binderMethod.invokeForRequest(request, null, dataBinder);
        // 如果@InitBinder注解的方法有返回值則拋出異常
        if (returnValue != null) {
          throw new IllegalStateException("@InitBinder methods must not return a value (should be void): " + binderMethod);
        }
      }
    }
  }
}
// 解析@InitBinder注解方法的參數(shù)及方法執(zhí)行
public class InvocableHandlerMethod extends HandlerMethod {
  public Object invokeForRequest(NativeWebRequest request, @Nullable ModelAndViewContainer mavContainer, Object... providedArgs) throws Exception {
    // 解析獲取@InitBinder注解方法的參數(shù)
    Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
    // 執(zhí)行調(diào)用
    return doInvoke(args);
  }
}

執(zhí)行類型轉(zhuǎn)換

在上面執(zhí)行流程中,我們知道獲取了一個WebDataBinder對象和由@InitBinder 注解的方法的調(diào)用執(zhí)行。接下來就是進行類型的轉(zhuǎn)換。

public abstract class AbstractNamedValueMethodArgumentResolver {
  public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
    if (binderFactory != null) {
      // 根據(jù)當前的Request對象及請求參數(shù)名創(chuàng)建WebDataBinder對象
      // 內(nèi)部創(chuàng)建的ExtendedServletRequestDataBinder對象
      WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
      try {
        // 執(zhí)行類型轉(zhuǎn)換
        arg = binder.convertIfNecessary(arg, parameter.getParameterType(), parameter);
      }
    }
  }
}
// 最終通過該類調(diào)用類型轉(zhuǎn)換
class TypeConverterDelegate {
  public <T> T convertIfNecessary(@Nullable String propertyName, @Nullable Object oldValue, @Nullable Object newValue, @Nullable Class<T> requiredType, @Nullable TypeDescriptor typeDescriptor) throws IllegalArgumentException {
    // Custom editor for this type?
    // 獲取自定義的類型轉(zhuǎn)換器(首先獲取的就是我們上面自定義的)
    PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);
    // ...
    Object convertedValue = newValue;
    // ...
    convertedValue = doConvertValue(oldValue, convertedValue, requiredType, editor);
  }
  private Object doConvertValue(@Nullable Object oldValue, @Nullable Object newValue, @Nullable Class<?> requiredType, @Nullable PropertyEditor editor) { 
    // ...
    if (convertedValue instanceof String) {
      if (editor != null) {
        String newTextValue = (String) convertedValue;
        // 最終的調(diào)用
        return doConvertTextValue(oldValue, newTextValue, editor);
      } else if (String.class == requiredType) {
        returnValue = convertedValue;
      }
    }
    return returnValue;
  }
  // 最終得到了我們想要的值
  private Object doConvertTextValue(@Nullable Object oldValue, String newTextValue, PropertyEditor editor) {
    try {
      editor.setValue(oldValue);
    }
    // ...
    editor.setAsText(newTextValue);
    return editor.getValue();
  }
}

以上就是參數(shù)綁定及類型轉(zhuǎn)換的過程。

到此這篇關(guān)于一文搞懂SpringMVC中@InitBinder注解的使用的文章就介紹到這了,更多相關(guān)SpringMVC @InitBinder注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中關(guān)于子類覆蓋父類的拋出異常問題

    Java中關(guān)于子類覆蓋父類的拋出異常問題

    今天小編就為大家分享一篇關(guān)于Java中關(guān)于子類覆蓋父類的拋出異常問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • Java反射如何修改private final成員變量值

    Java反射如何修改private final成員變量值

    這篇文章主要介紹了Java反射如何修改private final成員變量值,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • 利用Java巧妙解決Excel公式迭代計算

    利用Java巧妙解決Excel公式迭代計算

    迭代計算其實是在?Excel?中,一種公式的循環(huán)引用,那么如何利用Java語言巧妙解決Excel公式迭代計算的問題呢,下面小編就來和大家詳細講講吧
    2023-10-10
  • idea使用spring Initializr 快速搭建springboot項目遇到的坑

    idea使用spring Initializr 快速搭建springboot項目遇到的坑

    這篇文章主要介紹了idea使用spring Initializr 快速搭建springboot項目遇到的坑,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Java?String源碼contains題解重復(fù)疊加字符串匹配

    Java?String源碼contains題解重復(fù)疊加字符串匹配

    這篇文章主要為大家介紹了Java?String源碼contains題解重復(fù)疊加字符串匹配示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Spring batch批處理框架

    Spring batch批處理框架

    本文主要介紹了Spring batch批處理框架的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-04-04
  • 使用mybatis-plus報錯Invalid bound statement (not found)錯誤

    使用mybatis-plus報錯Invalid bound statement (not found)錯誤

    這篇文章主要介紹了使用mybatis-plus報錯Invalid bound statement (not found)錯誤,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Eclipse下編寫java程序突然不會自動生成R.java文件和包的解決辦法

    Eclipse下編寫java程序突然不會自動生成R.java文件和包的解決辦法

    這篇文章主要介紹了Eclipse下編寫java程序突然不會自動生成R.java文件和包的解決辦法 的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 解決執(zhí)行Junit單元測試報錯java.lang.ClassNotFoundException問題

    解決執(zhí)行Junit單元測試報錯java.lang.ClassNotFoundException問題

    這篇文章主要介紹了解決執(zhí)行Junit單元測試報錯java.lang.ClassNotFoundException問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java中的鎖分類的詳細介紹

    Java中的鎖分類的詳細介紹

    這篇文章主要介紹了Java中的鎖分類的詳細介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論