Skip to content

Commit fe124ca

Browse files
christophstroblmp911de
authored andcommitted
Eagerly initialize SimplePropertyValueConversions.
Closes #2590 Original pull request: #2591.
1 parent 37b072f commit fe124ca

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ public ConverterConfiguration(StoreConversions storeConversions, List<?> userCon
947947
public ConverterConfiguration(StoreConversions storeConversions, List<?> userConverters,
948948
Predicate<ConvertiblePair> converterRegistrationFilter) {
949949

950-
this(storeConversions, userConverters, converterRegistrationFilter, new SimplePropertyValueConversions());
950+
this(storeConversions, userConverters, converterRegistrationFilter, PropertyValueConversions.simple(it -> {}));
951951
}
952952

953953
/**

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

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ static <P extends PersistentProperty<P>> PropertyValueConversions simple(
6363
PropertyValueConverterRegistrar registrar = new PropertyValueConverterRegistrar();
6464
config.accept(registrar);
6565
conversions.setValueConverterRegistry(registrar.buildRegistry());
66+
try {
67+
conversions.afterPropertiesSet();
68+
} catch (Exception e) {
69+
throw new IllegalStateException("Could not initialize value conversions.");
70+
}
6671
return conversions;
6772
}
6873
}

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

+14-29
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
20-
import java.util.concurrent.atomic.AtomicBoolean;
2120

2221
import org.springframework.beans.factory.InitializingBean;
2322
import org.springframework.data.convert.PropertyValueConverterFactories.ChainedPropertyValueConverterFactory;
@@ -40,7 +39,6 @@ public class SimplePropertyValueConversions implements PropertyValueConversions,
4039
private @Nullable PropertyValueConverterFactory converterFactory;
4140
private @Nullable ValueConverterRegistry<?> valueConverterRegistry;
4241
private boolean converterCacheEnabled = true;
43-
private final AtomicBoolean initialized = new AtomicBoolean(false);
4442

4543
/**
4644
* Set the {@link PropertyValueConverterFactory factory} responsible for creating the actual
@@ -91,23 +89,13 @@ public void setConverterCacheEnabled(boolean converterCacheEnabled) {
9189

9290
@Override
9391
public boolean hasValueConverter(PersistentProperty<?> property) {
94-
95-
if (!initialized.get()) {
96-
init();
97-
}
98-
9992
return this.converterFactory.getConverter(property) != null;
10093
}
10194

10295
@Nullable
10396
@Override
10497
public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, D> getValueConverter(
10598
C property) {
106-
107-
if (!initialized.get()) {
108-
init();
109-
}
110-
11199
return this.converterFactory.getConverter(property);
112100
}
113101

@@ -116,27 +104,24 @@ public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContex
116104
*/
117105
public void init() {
118106

119-
if (initialized.compareAndSet(false, true)) {
120-
121-
List<PropertyValueConverterFactory> factoryList = new ArrayList<>(3);
107+
List<PropertyValueConverterFactory> factoryList = new ArrayList<>(3);
122108

123-
if (converterFactory != null) {
124-
factoryList.add(converterFactory);
125-
} else {
126-
factoryList.add(PropertyValueConverterFactory.simple());
127-
}
109+
if (converterFactory != null) {
110+
factoryList.add(converterFactory);
111+
} else {
112+
factoryList.add(PropertyValueConverterFactory.simple());
113+
}
128114

129-
if ((valueConverterRegistry != null) && !valueConverterRegistry.isEmpty()) {
130-
factoryList.add(PropertyValueConverterFactory.configuredInstance(valueConverterRegistry));
131-
}
115+
if ((valueConverterRegistry != null) && !valueConverterRegistry.isEmpty()) {
116+
factoryList.add(PropertyValueConverterFactory.configuredInstance(valueConverterRegistry));
117+
}
132118

133-
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
134-
? PropertyValueConverterFactory.chained(factoryList)
135-
: factoryList.iterator().next();
119+
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
120+
? PropertyValueConverterFactory.chained(factoryList)
121+
: factoryList.iterator().next();
136122

137-
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
138-
: targetFactory;
139-
}
123+
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
124+
: targetFactory;
140125
}
141126

142127
@Override

0 commit comments

Comments
 (0)