Eagerly initialize SimplePropertyValueConversions.

Closes #2590
Original pull request: #2591.
This commit is contained in:
Christoph Strobl
2022-04-04 10:15:33 +02:00
committed by Mark Paluch
parent 3e9451c939
commit b9eb4d1416
3 changed files with 22 additions and 32 deletions

View File

@@ -932,7 +932,7 @@ public class CustomConversions {
public ConverterConfiguration(StoreConversions storeConversions, List<?> userConverters,
Predicate<ConvertiblePair> converterRegistrationFilter) {
this(storeConversions, userConverters, converterRegistrationFilter, new SimplePropertyValueConversions());
this(storeConversions, userConverters, converterRegistrationFilter, PropertyValueConversions.simple(it -> {}));
}
/**

View File

@@ -63,6 +63,11 @@ public interface PropertyValueConversions {
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.");
}
return conversions;
}
}

View File

@@ -17,7 +17,6 @@ package org.springframework.data.convert;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.convert.PropertyValueConverterFactories.ChainedPropertyValueConverterFactory;
@@ -40,7 +39,6 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
private @Nullable PropertyValueConverterFactory converterFactory;
private @Nullable ValueConverterRegistry<?> valueConverterRegistry;
private boolean converterCacheEnabled = true;
private final AtomicBoolean initialized = new AtomicBoolean(false);
/**
* Set the {@link PropertyValueConverterFactory factory} responsible for creating the actual
@@ -91,11 +89,6 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
@Override
public boolean hasValueConverter(PersistentProperty<?> property) {
if (!initialized.get()) {
init();
}
return this.converterFactory.getConverter(property) != null;
}
@@ -103,11 +96,6 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
@Override
public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, D> getValueConverter(
C property) {
if (!initialized.get()) {
init();
}
return this.converterFactory.getConverter(property);
}
@@ -116,27 +104,24 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
*/
public void init() {
if (initialized.compareAndSet(false, true)) {
List<PropertyValueConverterFactory> factoryList = new ArrayList<>(3);
List<PropertyValueConverterFactory> factoryList = new ArrayList<>(3);
if (converterFactory != null) {
factoryList.add(converterFactory);
} else {
factoryList.add(PropertyValueConverterFactory.simple());
}
if ((valueConverterRegistry != null) && !valueConverterRegistry.isEmpty()) {
factoryList.add(PropertyValueConverterFactory.configuredInstance(valueConverterRegistry));
}
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
? PropertyValueConverterFactory.chained(factoryList)
: factoryList.iterator().next();
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
: targetFactory;
if (converterFactory != null) {
factoryList.add(converterFactory);
} else {
factoryList.add(PropertyValueConverterFactory.simple());
}
if ((valueConverterRegistry != null) && !valueConverterRegistry.isEmpty()) {
factoryList.add(PropertyValueConverterFactory.configuredInstance(valueConverterRegistry));
}
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
? PropertyValueConverterFactory.chained(factoryList)
: factoryList.iterator().next();
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
: targetFactory;
}
@Override