spring mvc one init binder适用于所有控制器 [英] spring mvc one init binder for all controllers
问题描述
我有5个控制器,我想向所有人注册 InitBinder
。
I have 5 controllers and i would like to register an InitBinder
to all of them.
我知道我可以将这个代码添加到每个代码中。
I know i can add this code to each of them.
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.registerCustomEditor(StringWrapper.class, new StringWrapperEditor());
}
但是我只想定义一次(甚至创建一个 StringWrapperEditor
的bean,并使用它而不是每次都创建新的。)
But i would like to define it only once (even create a bean of StringWrapperEditor
and use it instead of creating new every time.)
我搜索了SO和其他一些地方,但没有发现任何安装。
甚至可能吗?
I searched SO and some other places but didn't find any answear. Is it even possible?
我使用带有java 1.6的spring 3.1.1。
Im using spring 3.1.1 with java 1.6.
推荐答案
实现 PropertyEditorRegistrar
,它会注册所有自定义 PropertyEditors
。然后在你的配置中添加一个 ConfigurableWebBindingInitializer
,你可以用创建的 PropertyEditorRegistrar
连接它并将它挂钩到你的的HandlerAdapter
。
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());
}
}
如果您有< ; mvc:annotation-driven />
标签在您的配置中,问题是使用此标记您无法将 WebBindingInitializer
添加到适配器已经有一个 ConfigurableWebBindingInitializer
添加到预先配置的HandlerAdapter中。您可以使用 BeanPostProcessor
来处理和配置bean。
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());
}
}
}
}
需要一些工作,但它是可行的。您还可以实现自己的 WebBindingInitializer
。
Requires a bit of work but it is doable. You could also implement your own WebBindingInitializer
.
如果您没有标签,则只需手动配置 RequestMappingHandlerAdapter
并将所有内容连接在一起。
If you don't have the tag you can simply manually configure a RequestMappingHandlerAdapter
and wire everything together.
链接
- PropertyEditorRegistrar javadoc
- ConfigurableWebBindingInitializer javadoc
- Reference Guide link
这篇关于spring mvc one init binder适用于所有控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!