没有在spring mvc web app中调用BeanPostProcessor

时间:2014-03-26 06:17:02

标签: spring-mvc

我有一个spring mvc 3.0 Web应用程序,我正在尝试为initBinder方法实现通用解决方案。而不是在我的所有控制器中为我的日期选择器指定相同的initBinder代码,我试图这样做(我没有@Controller建议来帮助我在这个版本的春天)

@Component
public class MyPostProcessor implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
        if (bean instanceof RequestMappingHandlerAdapter) {
            WebBindingInitializer wbi = ((RequestMappingHandlerAdapter) bean).getWebBindingInitializer();
            if (wbi == null) {
                wbi = new ConfigurableWebBindingInitializer();
                ((RequestMappingHandlerAdapter) bean).setWebBindingInitializer(wbi);
            }

            if (wbi instanceof ConfigurableWebBindingInitializer) {
                ((ConfigurableWebBindingInitializer) wbi).setPropertyEditorRegistrar(new MyPropertyEditorRegistrar());
            }

        }
        return bean;
    }


public class MyPropertyEditorRegistrar implements PropertyEditorRegistrar {

    @Value("#{appProperties['date.format']}")
    public String dateFormat = null;

    /* (non-Javadoc)
     * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
     */
     public void registerCustomEditors(PropertyEditorRegistry registry) {
            SimpleDateFormat df = new SimpleDateFormat(dateFormat);
            df.setLenient(false);
            registry.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
     }
}

但我的BeanPostProcessor似乎没有被调用。有人可以帮我理解BeanPostProcessor的调用方式和时间吗?

P.S。我正在使用mvc:annotationdriven

感谢

0 个答案:

没有答案