java – 如何将自定义版本的WebDataBinder注入Spring 3 MVC?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何将自定义版本的WebDataBinder注入Spring 3 MVC?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6271字,纯文字阅读大概需要9分钟。
内容图文
![java – 如何将自定义版本的WebDataBinder注入Spring 3 MVC?](/upload/InfoBanner/zyjiaocheng/824/f490be225d2e417a8e844f2495893af3.jpg)
我写了一个WebDataBinder的自定义实现.在将来,我想对它进行增强,以便它在类本身上查找注释,并确定它是否应该绑定数据.
如何将此类注入Spring上下文而不是WebDataBinder?
我想要的是,如果我运行此代码,我的WebDataBinder版本将被注入,而不是默认的Spring.
@Controller
public class MyFormController {
@InitBinder
public void initBinder(WebDataBinder binder) {
// ...
}
// ...
}
我自定义的WebDataBinder实现.它允许我按类而不是方法名称排除数据绑定.
package com.companyname.spring;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomDataBinder extends WebDataBinder {
List<Class> disallowedClasses = new ArrayList<>();
public CustomDataBinder(Object target) {
super(target);
}
public CustomDataBinder(Object target, String objectName) {
super(target, objectName);
}
public CustomDataBinder disallowClass(Class... classes) {
Collections.addAll(disallowedClasses, classes);
return this;
}
@Override
protected void doBind(MutablePropertyValues mpvs) {
if(disallowedClasses.contains(getTarget().getClass())) {
if (logger.isDebugEnabled()) {
logger.debug("DataBinder will not bind class [" + getTarget().getClass().getSimpleName() + "] because it appears in the list of disallowed classes [" + StringUtils.collectionToCommaDelimitedString(disallowedClasses) + "]");
}
} else {
super.doBind(mpvs);
}
}
}
EDIT1
第一次通过,遇到AsyncSupportConfigurer问题
@Configuration
@ComponentScan(basePackageClasses = RootContextConfig.class)
@EnableTransactionManagement
@EnableWebSecurity
@EnableAsync
@EnableSpringConfigured
@EnableLoadTimeWeaving
public class RootContextConfig extends WebMvcConfigurationSupport {
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<HandlerMethodArgumentResolver>();
addArgumentResolvers(argumentResolvers);
List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<HandlerMethodReturnValueHandler>();
addReturnValueHandlers(returnValueHandlers);
RequestMappingHandlerAdapter adapter = new CustomRequestMappingHandlerAdapter();
adapter.setContentNegotiationManager(mvcContentNegotiationManager());
adapter.setMessageConverters(getMessageConverters());
adapter.setWebBindingInitializer(getConfigurableWebBindingInitializer());
adapter.setCustomArgumentResolvers(argumentResolvers);
adapter.setCustomReturnValueHandlers(returnValueHandlers);
AsyncSupportConfigurer configurer = new AsyncSupportConfigurer();
configureAsyncSupport(configurer);
//All the methods called off of configurer are giving me errors because they have protected level access. I'm not really sure how they're being called in the code I copied this from.
if (configurer.getTaskExecutor() != null) {
adapter.setTaskExecutor(configurer.getTaskExecutor());
}
if (configurer.getTimeout() != null) {
adapter.setAsyncRequestTimeout(configurer.getTimeout());
}
adapter.setCallableInterceptors(configurer.getCallableInterceptors());
adapter.setDeferredResultInterceptors(configurer.getDeferredResultInterceptors());
return adapter;
}
自定义RequestMappingHandlerAdapter
package com.companyname.dirtylibs.spring;
import org.springframework.web.method.annotation.InitBinderDataBinderFactory;
import org.springframework.web.method.support.InvocableHandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletRequestDataBinderFactory;
import java.util.List;
public class CustomRequestMappingHandlerAdapter extends RequestMappingHandlerAdapter {
@Override
protected InitBinderDataBinderFactory createDataBinderFactory(List<InvocableHandlerMethod> binderMethods) throws Exception {
return new CustomInitBinderDataBinderFactory(binderMethods, getWebBindingInitializer());
}
}
自定义InitBinderDataBinderFactory
package com.companyname.dirtylibs.spring;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.bind.support.WebRequestDataBinder;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.annotation.InitBinderDataBinderFactory;
import org.springframework.web.method.support.InvocableHandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.ServletRequestDataBinderFactory;
import java.util.List;
public class CustomInitBinderDataBinderFactory extends InitBinderDataBinderFactory {
/**
* Create a new instance.
*
* @param binderMethods {@code @InitBinder} methods, or {@code null}
* @param initializer for global data binder intialization
*/
public CustomInitBinderDataBinderFactory(List<InvocableHandlerMethod> binderMethods, WebBindingInitializer initializer) {
super(binderMethods, initializer);
}
@Override
protected CustomDataBinder createBinderInstance(Object target, String objectName, NativeWebRequest webRequest) throws Exception {
return new CustomDataBinder(target, objectName);
}
}
解决方法:
这不是一项简单的任务. Spring可以进行大量的自定义,但是,该死的,这种变化并不好玩.
您需要扩展RequestMappingHandlerAdapter类并覆盖following method
/**
* Template method to create a new InitBinderDataBinderFactory instance.
* <p>The default implementation creates a ServletRequestDataBinderFactory.
* This can be overridden for custom ServletRequestDataBinder subclasses.
* @param binderMethods {@code @InitBinder} methods
* @return the InitBinderDataBinderFactory instance to use
* @throws Exception in case of invalid state or arguments
*/
protected InitBinderDataBinderFactory createDataBinderFactory(List<InvocableHandlerMethod> binderMethods)
throws Exception {
return new ServletRequestDataBinderFactory(binderMethods, getWebBindingInitializer());
}
您需要返回返回自定义WebDataBinder实例的自定义InitBinderDataBinderFactory,而不是返回ServletRequestDataBinderFactory.
此更改意味着您无法使用默认的@EnableWebMvc或< mvc:annotation-driven />组态.那是因为他们默认使用RequestMappingHandlerAdapter,但是你需要注册自己的类.
但是,您可以覆盖@Bean带注释的WebMvcConfigurationSupport#requestMappingHandlerAdapter()方法,并提供您自己的实现以返回您自己的类型.查看该实现对提示的作用.
内容总结
以上是互联网集市为您收集整理的java – 如何将自定义版本的WebDataBinder注入Spring 3 MVC?全部内容,希望文章能够帮你解决java – 如何将自定义版本的WebDataBinder注入Spring 3 MVC?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。