是否有基于注释的方法在Spring MVC 3.0中全局注册PropertyEditors?

我想知道是否有一种方法可以在Spring MVC 3.0之内全局注册PropertyEditor。在他们的文档文档中,他们展示了如何使用注释来基于每个控制器自定义bean PropertyEditor,并且 - 在我看来 - 就像我在全球范围内这样做的XML方式。所以我想知道,有没有办法,只需使用注释为所有控制器注册PropertyEditors,而无需为每个控制器执行@InitBinder方法。使用@InitBinder方法创建公共超类也是不可取的。 在Spring 3.0发布之前,有人就此主题提出了另一个问题。     
已邀请:
package com.projectr.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;

/**
 * Shared WebBindingInitializer for custom property editors.
 * 
 * @author aramirez
 * 
 */
public class CommonBindingInitializer implements WebBindingInitializer {
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
        binder.registerCustomEditor(Long.class, null, new CustomNumberEditor( Long.class, null, true));
        binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
        binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale());
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
    }
}
在您的应用程序上下文或调度程序-servlet中
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
      <bean class="com.projectr.web.CommonBindingInitializer"/>
    </property>
  </bean>
注释相当于上面的代码。请注意,
@ControllerAdvice
是在Spring 3.2.x中引入的
@ControllerAdvice
public class CommonBindingInitializer {
  @InitBinder
  public void registerCustomEditors(WebDataBinder binder, WebRequest request) {
    binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
    binder.registerCustomEditor(Long.class, null, new CustomNumberEditor( Long.class, null, true));
    binder.registerCustomEditor(Boolean.class, null, new CustomBooleanEditor(true));
    binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", request.getLocale());
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
  }
}
    

要回复问题请先登录注册