From 22948bd7f04075a30c211c6b89cd014e09593382 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 20 Aug 2015 19:47:11 +0200 Subject: [PATCH] Add hook to create custom BeanPropertyBindingResult Issue: SPR-13373 --- .../validation/DataBinder.java | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/validation/DataBinder.java b/spring-context/src/main/java/org/springframework/validation/DataBinder.java index edd50c730d..779f042cb4 100644 --- a/spring-context/src/main/java/org/springframework/validation/DataBinder.java +++ b/spring-context/src/main/java/org/springframework/validation/DataBinder.java @@ -98,6 +98,7 @@ import org.springframework.util.StringUtils; * @author Rod Johnson * @author Juergen Hoeller * @author Rob Harrop + * @author Stephane Nicoll * @see #setAllowedFields * @see #setRequiredFields * @see #registerCustomEditor @@ -250,29 +251,50 @@ public class DataBinder implements PropertyEditorRegistry, TypeConverter { * Initialize standard JavaBean property access for this DataBinder. *

This is the default; an explicit call just leads to eager initialization. * @see #initDirectFieldAccess() + * @see #createBeanPropertyBindingResult() */ public void initBeanPropertyAccess() { Assert.state(this.bindingResult == null, "DataBinder is already initialized - call initBeanPropertyAccess before other configuration methods"); - this.bindingResult = new BeanPropertyBindingResult( - getTarget(), getObjectName(), isAutoGrowNestedPaths(), getAutoGrowCollectionLimit()); + this.bindingResult = createBeanPropertyBindingResult(); + } + + /** + * Create the {@link AbstractPropertyBindingResult} instance using standard JavaBean + * property access. + */ + protected AbstractPropertyBindingResult createBeanPropertyBindingResult() { + BeanPropertyBindingResult result = new BeanPropertyBindingResult(getTarget(), + getObjectName(), isAutoGrowNestedPaths(), getAutoGrowCollectionLimit()); if (this.conversionService != null) { - this.bindingResult.initConversion(this.conversionService); + result.initConversion(this.conversionService); } + return result; } /** * Initialize direct field access for this DataBinder, * as alternative to the default bean property access. * @see #initBeanPropertyAccess() + * @see #createDirectFieldBindingResult() */ public void initDirectFieldAccess() { Assert.state(this.bindingResult == null, "DataBinder is already initialized - call initDirectFieldAccess before other configuration methods"); - this.bindingResult = new DirectFieldBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths()); + this.bindingResult = createDirectFieldBindingResult(); + } + + /** + * Create the {@link AbstractPropertyBindingResult} instance using direct field + * access. + */ + protected AbstractPropertyBindingResult createDirectFieldBindingResult() { + DirectFieldBindingResult result = new DirectFieldBindingResult(getTarget(), + getObjectName(), isAutoGrowNestedPaths()); if (this.conversionService != null) { - this.bindingResult.initConversion(this.conversionService); + result.initConversion(this.conversionService); } + return result; } /**