Skip to content

Commit 477c69f

Browse files
Synchronize initialization of SimplePropertyValueConversions.
1 parent 5b1d58e commit 477c69f

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

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

Lines changed: 23 additions & 21 deletions
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,7 @@ 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);
42+
private boolean initialized = false;
4443

4544
/**
4645
* Set the {@link PropertyValueConverterFactory factory} responsible for creating the actual
@@ -92,7 +91,7 @@ public void setConverterCacheEnabled(boolean converterCacheEnabled) {
9291
@Override
9392
public boolean hasValueConverter(PersistentProperty<?> property) {
9493

95-
if (!initialized.get()) {
94+
if (!initialized) {
9695
init();
9796
}
9897

@@ -104,7 +103,7 @@ public boolean hasValueConverter(PersistentProperty<?> property) {
104103
public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContext<C>> PropertyValueConverter<DV, SV, D> getValueConverter(
105104
C property) {
106105

107-
if (!initialized.get()) {
106+
if (!initialized) {
108107
init();
109108
}
110109

@@ -114,29 +113,32 @@ public <DV, SV, C extends PersistentProperty<C>, D extends ValueConversionContex
114113
/**
115114
* May be called just once to initialize the underlying factory with its values.
116115
*/
117-
public void init() {
116+
public synchronized void init() {
118117

119-
if (initialized.compareAndSet(false, true)) {
118+
if (initialized) {
119+
return;
120+
}
120121

121-
List<PropertyValueConverterFactory> factoryList = new ArrayList<>(3);
122+
List<PropertyValueConverterFactory> factoryList = new ArrayList<>(3);
122123

123-
if (converterFactory != null) {
124-
factoryList.add(converterFactory);
125-
} else {
126-
factoryList.add(PropertyValueConverterFactory.simple());
127-
}
124+
if (converterFactory != null) {
125+
factoryList.add(converterFactory);
126+
} else {
127+
factoryList.add(PropertyValueConverterFactory.simple());
128+
}
128129

129-
if ((valueConverterRegistry != null) && !valueConverterRegistry.isEmpty()) {
130-
factoryList.add(PropertyValueConverterFactory.configuredInstance(valueConverterRegistry));
131-
}
130+
if ((valueConverterRegistry != null) && !valueConverterRegistry.isEmpty()) {
131+
factoryList.add(PropertyValueConverterFactory.configuredInstance(valueConverterRegistry));
132+
}
132133

133-
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
134-
? PropertyValueConverterFactory.chained(factoryList)
135-
: factoryList.iterator().next();
134+
PropertyValueConverterFactory targetFactory = factoryList.size() > 1
135+
? PropertyValueConverterFactory.chained(factoryList)
136+
: factoryList.iterator().next();
136137

137-
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
138-
: targetFactory;
139-
}
138+
this.converterFactory = converterCacheEnabled ? PropertyValueConverterFactory.caching(targetFactory)
139+
: targetFactory;
140+
141+
initialized = true;
140142
}
141143

142144
@Override

0 commit comments

Comments
 (0)