You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC:使用@InitBinder无法修改BeanPropertyBindingResult的autoGrowCollectionLimit

解决方案(Spring 4.1.7)

问题出在方法调用顺序上:initBeanPropertyAccess()必须在所有配置方法之前调用,一旦WebDataBinder完成初始化(比如创建了BeanPropertyBindingResult实例),再调用该方法就会触发报错。

修改你的@InitBinder方法,调整调用顺序即可:

@InitBinder
public void registerBeanEditors(WebDataBinder binder) throws TacException {
        binder.initBeanPropertyAccess();
        binder.setAutoGrowCollectionLimit(4096);
}

原理说明

在Spring 4.1.7中,WebDataBinder的initBeanPropertyAccess()方法会初始化内部的BeanPropertyBindingResult实例。此时设置autoGrowCollectionLimit,这个值会被同步到刚创建的BeanPropertyBindingResult中。如果先调用setAutoGrowCollectionLimit(),WebDataBinder会提前完成初始化(创建默认的绑定结果对象),后续再调用initBeanPropertyAccess()就会触发"DataBinder is already initialized"的错误。

如果需要全局统一配置,避免在每个Controller的@InitBinder中重复设置,还可以自定义WebBindingInitializer:

public class CustomWebBindingInitializer implements WebBindingInitializer {
    @Override
    public void initBinder(WebDataBinder binder, WebRequest request) {
        binder.initBeanPropertyAccess();
        binder.setAutoGrowCollectionLimit(4096);
    }
}

然后在Spring MVC配置中注册该初始化器:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="com.yourpackage.CustomWebBindingInitializer"/>
    </property>
</bean>

内容的提问来源于stack exchange,提问作者NICO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 06:53:07