diff --git a/src/main/java/org/springframework/data/convert/PropertyValueConversions.java b/src/main/java/org/springframework/data/convert/PropertyValueConversions.java index 981081781..cfb55190e 100644 --- a/src/main/java/org/springframework/data/convert/PropertyValueConversions.java +++ b/src/main/java/org/springframework/data/convert/PropertyValueConversions.java @@ -61,13 +61,11 @@ public interface PropertyValueConversions { SimplePropertyValueConversions conversions = new SimplePropertyValueConversions(); PropertyValueConverterRegistrar registrar = new PropertyValueConverterRegistrar(); + config.accept(registrar); conversions.setValueConverterRegistry(registrar.buildRegistry()); - try { - conversions.afterPropertiesSet(); - } catch (Exception e) { - throw new IllegalStateException("Could not initialize value conversions."); - } + conversions.afterPropertiesSet(); + return conversions; } } diff --git a/src/main/java/org/springframework/data/convert/SimplePropertyValueConversions.java b/src/main/java/org/springframework/data/convert/SimplePropertyValueConversions.java index 4cc3435ad..e1a128f3a 100644 --- a/src/main/java/org/springframework/data/convert/SimplePropertyValueConversions.java +++ b/src/main/java/org/springframework/data/convert/SimplePropertyValueConversions.java @@ -32,6 +32,7 @@ import org.springframework.lang.Nullable; * {@link PropertyValueConverter converter} retrieval. * * @author Christoph Strobl + * @author Mark Paluch * @since 2.7 */ public class SimplePropertyValueConversions implements PropertyValueConversions, InitializingBean { @@ -55,6 +56,18 @@ public class SimplePropertyValueConversions implements PropertyValueConversions, return converterFactory; } + private PropertyValueConverterFactory obtainConverterFactory() { + + PropertyValueConverterFactory factory = getConverterFactory(); + + if (factory == null) { + throw new IllegalStateException( + "PropertyValueConverterFactory is not set. Make sure to either set the converter factory or call afterPropertiesSet() to initialize the object."); + } + + return factory; + } + /** * Set the {@link ValueConverterRegistry converter registry} for path configured converters. This is short for adding * a @@ -78,7 +91,7 @@ public class SimplePropertyValueConversions implements PropertyValueConversions, } /** - * Dis-/Enable caching. Enabled by default. + * Configure whether to use converter cache. Enabled by default. * * @param converterCacheEnabled set to {@literal true} to enable caching of {@link PropertyValueConverter converter} * instances. @@ -89,14 +102,14 @@ public class SimplePropertyValueConversions implements PropertyValueConversions, @Override public boolean hasValueConverter(PersistentProperty property) { - return this.converterFactory.getConverter(property) != null; + return obtainConverterFactory().getConverter(property) != null; } @Nullable @Override public , D extends ValueConversionContext> PropertyValueConverter getValueConverter( C property) { - return this.converterFactory.getConverter(property); + return obtainConverterFactory().getConverter(property); } /** @@ -125,7 +138,7 @@ public class SimplePropertyValueConversions implements PropertyValueConversions, } @Override - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { init(); } }