gpt4 book ai didi

java - 如何将自定义版本的 WebDataBinder 注入(inject) Spring 3 MVC?

转载 作者:搜寻专家 更新时间:2023-10-31 20:02:34 32 4
gpt4 key购买 nike

我编写了 WebDataBinder 的自定义实现。将来我想增强它,以便它在类本身上查找注释,并确定它是否应该将数据绑定(bind)到它。

如何将此类注入(inject)到 Spring 上下文中以代替 WebDataBinder?

我想要的是,如果我运行这段代码,我的 WebDataBinder 版本将被注入(inject),而不是默认的 Spring 版本。

@Controller
public class MyFormController {

@InitBinder
public void initBinder(WebDataBinder binder) {
// ...
}

// ...
}

我自定义的 WebDataBinder 实现。它让我可以排除按类而不是按方法名称的数据绑定(bind)。

    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);
}
}
}

编辑1

第一步,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());
}

而不是返回 ServletRequestDataBinderFactory ,您需要返回自定义 InitBinderDataBinderFactory返回您的自定义 WebDataBinder实例。

此更改意味着您不能使用默认的 @EnableWebMvc<mvc:annotation-driven/>配置。那是因为他们使用 RequestMappingHandlerAdapter默认情况下,但您需要注册自己的类(class)。

但是,您可以覆盖 @Bean注释 WebMvcConfigurationSupport#requestMappingHandlerAdapter()方法并提供您自己的实现以返回您自己的类型。查看该实现的提示。

关于java - 如何将自定义版本的 WebDataBinder 注入(inject) Spring 3 MVC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22520496/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com