亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

環(huán)境:Springboot2.4.12

簡介

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

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

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

@InitBinder方法支持許多與@RequestMapping方法相同的參數(shù),除了@ModelAttribute(命令對(duì)象)參數(shù)。通常,它們是用WebDataBinder參數(shù)(用于注冊(cè))和一個(gè)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 ;
  }
}

注意以下幾點(diǎn):

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

在上面的示例中注冊(cè)了一個(gè)類型轉(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 {
    // ...
    // 這里會(huì)查找當(dāng)前執(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ù)名對(duì)應(yīng)的請(qǐng)求參數(shù)值:/demos/index?id=100 , 這就返回100
    Object arg = resolveName(resolvedName.toString(), nestedParameter, webRequest);
    // ...
    if (binderFactory != null) {
      // 根據(jù)當(dāng)前的Request對(duì)象及請(qǐng)求參數(shù)名創(chuàng)建WebDataBinder對(duì)象
      // 內(nèi)部創(chuàng)建的ExtendedServletRequestDataBinder對(duì)象
      WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
      try {
        // 執(zhí)行類型轉(zhuǎn)換
        arg = binder.convertIfNecessary(arg, parameter.getParameterType(), parameter);
      }
    }
  }
}
// 創(chuàng)建WebDataBinder對(duì)象
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對(duì)象,這里最主要的就是為其設(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í)行流程中,我們知道獲取了一個(gè)WebDataBinder對(duì)象和由@InitBinder 注解的方法的調(diào)用執(zhí)行。接下來就是進(jìn)行類型的轉(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ù)當(dāng)前的Request對(duì)象及請(qǐng)求參數(shù)名創(chuàng)建WebDataBinder對(duì)象
      // 內(nèi)部創(chuàng)建的ExtendedServletRequestDataBinder對(duì)象
      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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論