Browse Source

Eagerly initialize SimplePropertyValueConversions.

Closes #2590
Original pull request: #2591.
pull/2592/head
Christoph Strobl 4 years ago committed by Mark Paluch
parent
commit
fe124ca598
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 2
      src/main/java/org/springframework/data/convert/CustomConversions.java
  2. 5
      src/main/java/org/springframework/data/convert/PropertyValueConversions.java
  3. 43
      src/main/java/org/springframework/data/convert/SimplePropertyValueConversions.java

2
src/main/java/org/springframework/data/convert/CustomConversions.java

@ -947,7 +947,7 @@ public class CustomConversions { @@ -947,7 +947,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 -> {}));
}
/**

5
src/main/java/org/springframework/data/convert/PropertyValueConversions.java

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

43
src/main/java/org/springframework/data/convert/SimplePropertyValueConversions.java

@ -17,7 +17,6 @@ package org.springframework.data.convert; @@ -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, @@ -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, @@ -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, @@ -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, @@ -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 (converterFactory != null) {
factoryList.add(converterFactory);
} else {
factoryList.add(PropertyValueConverterFactory.simple());
}
if ((valueConverterRegistry != null) && !valueConverterRegistry.isEmpty()) {
factoryList.add(PropertyValueConverterFactory.configuredInstance(valueConverterRegistry));
}
if ((valueConverterRegistry != null) && !valueConverterRegistry.isEmpty()) {
factoryList.add(PropertyValueConverterFactory.configuredInstance(valueConverterRegistry));
}
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
? PropertyValueConverterFactory.chained(factoryList)
: factoryList.iterator().next();
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
? PropertyValueConverterFactory.chained(factoryList)
: factoryList.iterator().next();
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
: targetFactory;
}
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
: targetFactory;
}
@Override

Loading…
Cancel
Save