Implement a PropertyEditorRegistrar
which registers all your custom PropertyEditors
. Then in your configuration add a ConfigurableWebBindingInitializer
which you hookup with the created PropertyEditorRegistrar
and hook it to your HandlerAdapter
.
public class MyPropertyEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(StringWrapper.class, new StringWrapperEditor());
}
}
If you have a <mvc:annotation-driven />
tag in your configuration, the problem is that with this tag you cannot add the WebBindingInitializer
to the adapter next to that there is already a ConfigurableWebBindingInitializer
added to the pre-configured HandlerAdapter. You can use a BeanPostProcessor
to proces and configure the bean.
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());
}
}
}
}
Requires a bit of work but it is doable. You could also implement your own WebBindingInitializer
.
If you don't have the tag you can simply manually configure a RequestMappingHandlerAdapter
and wire everything together.
Links