18
18
import java .util .Arrays ;
19
19
import java .util .Collections ;
20
20
import java .util .EnumSet ;
21
- import java .util .HashMap ;
22
21
import java .util .List ;
23
22
import java .util .Map ;
24
23
import java .util .Objects ;
25
24
import java .util .Optional ;
25
+ import java .util .concurrent .ConcurrentHashMap ;
26
26
27
27
import org .springframework .beans .BeanUtils ;
28
28
import org .springframework .beans .factory .BeanFactory ;
29
- import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
30
29
import org .springframework .beans .factory .config .AutowireCapableBeanFactory ;
31
30
import org .springframework .data .mapping .PersistentProperty ;
32
31
import org .springframework .lang .Nullable ;
@@ -120,29 +119,26 @@ static class BeanFactoryAwarePropertyValueConverterFactory implements PropertyVa
120
119
}
121
120
122
121
@ Override
123
- public <S , T , C extends ValueConversionContext <?>> PropertyValueConverter <S , T , C > getConverter (
124
- Class <? extends PropertyValueConverter <S , T , C >> converterType ) {
122
+ public <DV , SV , C extends ValueConversionContext <?>> PropertyValueConverter <DV , SV , C > getConverter (
123
+ Class <? extends PropertyValueConverter <DV , SV , C >> converterType ) {
125
124
126
- Assert .state (beanFactory != null , "BeanFactory must not be null. Did you forget to set it!" );
127
125
Assert .notNull (converterType , "ConverterType must not be null!" );
128
126
129
- try {
130
- return beanFactory .getBean (converterType );
131
- } catch (NoSuchBeanDefinitionException exception ) {
127
+ PropertyValueConverter <DV , SV , C > converter = beanFactory .getBeanProvider (converterType ).getIfAvailable ();
132
128
133
- if (beanFactory instanceof AutowireCapableBeanFactory ) {
134
- return (PropertyValueConverter <S , T , C >) ((AutowireCapableBeanFactory ) beanFactory ).createBean (converterType ,
135
- AutowireCapableBeanFactory .AUTOWIRE_CONSTRUCTOR , false );
136
- }
129
+ if (converter == null && beanFactory instanceof AutowireCapableBeanFactory ) {
130
+ return (PropertyValueConverter <DV , SV , C >) ((AutowireCapableBeanFactory ) beanFactory ).createBean (converterType ,
131
+ AutowireCapableBeanFactory .AUTOWIRE_CONSTRUCTOR , false );
137
132
}
138
- return null ;
133
+
134
+ return converter ;
139
135
}
140
136
}
141
137
142
138
/**
143
139
* {@link PropertyValueConverterFactory} implementation that serves {@link PropertyValueConverter} from a given
144
140
* {@link ValueConverterRegistry registry}.
145
- *
141
+ *
146
142
* @author Christoph Strobl
147
143
* @since 2.7
148
144
*/
@@ -158,9 +154,9 @@ static class ConfiguredInstanceServingValueConverterFactory implements PropertyV
158
154
159
155
@ Nullable
160
156
@ Override
161
- public <A , B , C extends ValueConversionContext <?>> PropertyValueConverter <A , B , C > getConverter (
157
+ public <DV , SV , C extends ValueConversionContext <?>> PropertyValueConverter <DV , SV , C > getConverter (
162
158
PersistentProperty <?> property ) {
163
- return (PropertyValueConverter <A , B , C >) converterRegistry .getConverter (property .getOwner ().getType (),
159
+ return (PropertyValueConverter <DV , SV , C >) converterRegistry .getConverter (property .getOwner ().getType (),
164
160
property .getName ());
165
161
}
166
162
@@ -174,7 +170,7 @@ public <S, T, C extends ValueConversionContext<?>> PropertyValueConverter<S, T,
174
170
/**
175
171
* {@link PropertyValueConverterFactory} implementation that caches converters provided by an underlying
176
172
* {@link PropertyValueConverterFactory factory}.
177
- *
173
+ *
178
174
* @author Christoph Strobl
179
175
* @since 2.7
180
176
*/
@@ -191,7 +187,7 @@ static class CachingPropertyValueConverterFactory implements PropertyValueConver
191
187
192
188
@ Nullable
193
189
@ Override
194
- public <S , T , C extends ValueConversionContext <?>> PropertyValueConverter <S , T , C > getConverter (
190
+ public <DV , SV , C extends ValueConversionContext <?>> PropertyValueConverter <DV , SV , C > getConverter (
195
191
PersistentProperty <?> property ) {
196
192
197
193
Optional <PropertyValueConverter <?, ?, ? extends ValueConversionContext <?>>> converter = cache .get (property );
@@ -201,8 +197,8 @@ public <S, T, C extends ValueConversionContext<?>> PropertyValueConverter<S, T,
201
197
}
202
198
203
199
@ Override
204
- public <S , T , C extends ValueConversionContext <?>> PropertyValueConverter <S , T , C > getConverter (
205
- Class <? extends PropertyValueConverter <S , T , C >> converterType ) {
200
+ public <DV , SV , C extends ValueConversionContext <?>> PropertyValueConverter <DV , SV , C > getConverter (
201
+ Class <? extends PropertyValueConverter <DV , SV , C >> converterType ) {
206
202
207
203
Optional <PropertyValueConverter <?, ?, ? extends ValueConversionContext <?>>> converter = cache .get (converterType );
208
204
@@ -212,8 +208,8 @@ public <S, T, C extends ValueConversionContext<?>> PropertyValueConverter<S, T,
212
208
213
209
static class Cache {
214
210
215
- Map <PersistentProperty <?>, Optional <PropertyValueConverter <?, ?, ? extends ValueConversionContext <?>>>> perPropertyCache = new HashMap <>();
216
- Map <Class <?>, Optional <PropertyValueConverter <?, ?, ? extends ValueConversionContext <?>>>> typeCache = new HashMap <>();
211
+ Map <PersistentProperty <?>, Optional <PropertyValueConverter <?, ?, ? extends ValueConversionContext <?>>>> perPropertyCache = new ConcurrentHashMap <>();
212
+ Map <Class <?>, Optional <PropertyValueConverter <?, ?, ? extends ValueConversionContext <?>>>> typeCache = new ConcurrentHashMap <>();
217
213
218
214
Optional <PropertyValueConverter <?, ?, ? extends ValueConversionContext <?>>> get (PersistentProperty <?> property ) {
219
215
return perPropertyCache .get (property );
@@ -226,7 +222,12 @@ static class Cache {
226
222
<S , T , C extends ValueConversionContext <?>> PropertyValueConverter <S , T , C > cache (PersistentProperty <?> property ,
227
223
@ Nullable PropertyValueConverter <S , T , C > converter ) {
228
224
perPropertyCache .putIfAbsent (property , Optional .ofNullable (converter ));
229
- cache (property .getValueConverterType (), converter );
225
+
226
+ Class <? extends PropertyValueConverter <?, ?, ? extends ValueConversionContext <? extends PersistentProperty <?>>>> valueConverterType = property
227
+ .getValueConverterType ();
228
+ if (valueConverterType != null ) {
229
+ cache (valueConverterType , converter );
230
+ }
230
231
return converter ;
231
232
}
232
233
0 commit comments