32
32
import org .springframework .beans .factory .BeanClassLoaderAware ;
33
33
import org .springframework .context .ApplicationContext ;
34
34
import org .springframework .context .ApplicationContextAware ;
35
+ import org .springframework .context .EnvironmentAware ;
35
36
import org .springframework .core .CollectionFactory ;
36
37
import org .springframework .core .convert .ConversionService ;
37
38
import org .springframework .core .convert .support .DefaultConversionService ;
39
+ import org .springframework .core .env .Environment ;
40
+ import org .springframework .core .env .EnvironmentCapable ;
41
+ import org .springframework .core .env .StandardEnvironment ;
38
42
import org .springframework .dao .InvalidDataAccessApiUsageException ;
39
43
import org .springframework .data .cassandra .core .mapping .*;
40
44
import org .springframework .data .cassandra .core .mapping .Embedded .OnEmpty ;
46
50
import org .springframework .data .mapping .PersistentProperty ;
47
51
import org .springframework .data .mapping .PersistentPropertyAccessor ;
48
52
import org .springframework .data .mapping .context .MappingContext ;
53
+ import org .springframework .data .mapping .model .CachingValueExpressionEvaluatorFactory ;
49
54
import org .springframework .data .mapping .model .ConvertingPropertyAccessor ;
50
- import org .springframework .data .mapping .model .DefaultSpELExpressionEvaluator ;
51
55
import org .springframework .data .mapping .model .EntityInstantiator ;
52
56
import org .springframework .data .mapping .model .ParameterValueProvider ;
53
57
import org .springframework .data .mapping .model .PropertyValueProvider ;
54
58
import org .springframework .data .mapping .model .SpELContext ;
55
- import org .springframework .data .mapping .model .SpELExpressionEvaluator ;
56
- import org .springframework .data .mapping .model .SpELExpressionParameterValueProvider ;
59
+ import org .springframework .data .mapping .model .ValueExpressionEvaluator ;
60
+ import org .springframework .data .mapping .model .ValueExpressionParameterValueProvider ;
57
61
import org .springframework .data .projection .EntityProjection ;
58
62
import org .springframework .data .projection .ProjectionFactory ;
59
63
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
60
64
import org .springframework .data .util .Predicates ;
61
65
import org .springframework .data .util .TypeInformation ;
66
+ import org .springframework .expression .spel .standard .SpelExpressionParser ;
62
67
import org .springframework .lang .Nullable ;
63
68
import org .springframework .util .Assert ;
64
69
import org .springframework .util .ClassUtils ;
87
92
* @author Frank Spitulski
88
93
*/
89
94
public class MappingCassandraConverter extends AbstractCassandraConverter
90
- implements ApplicationContextAware , BeanClassLoaderAware {
95
+ implements ApplicationContextAware , EnvironmentAware , EnvironmentCapable , BeanClassLoaderAware {
91
96
92
97
private final Log log = LogFactory .getLog (getClass ());
93
98
@@ -99,11 +104,21 @@ public class MappingCassandraConverter extends AbstractCassandraConverter
99
104
100
105
private @ Nullable ClassLoader beanClassLoader ;
101
106
107
+ private @ Nullable Environment environment ;
108
+
102
109
private SpELContext spELContext ;
103
110
104
111
private final DefaultColumnTypeResolver cassandraTypeResolver ;
112
+
105
113
private final EmbeddedEntityOperations embeddedEntityOperations ;
106
- private final SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory ();
114
+
115
+ private final SpelExpressionParser expressionParser = new SpelExpressionParser ();
116
+
117
+ private final SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory (
118
+ expressionParser );
119
+
120
+ private final CachingValueExpressionEvaluatorFactory expressionEvaluatorFactory = new CachingValueExpressionEvaluatorFactory (
121
+ expressionParser , this , o -> spELContext .getEvaluationContext (o ));
107
122
108
123
/**
109
124
* Create a new {@link MappingCassandraConverter} with a {@link CassandraMappingContext}.
@@ -171,10 +186,30 @@ private static CassandraMappingContext newDefaultMappingContext(CassandraCustomC
171
186
172
187
@ Override
173
188
public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
189
+
174
190
this .spELContext = new SpELContext (this .spELContext , applicationContext );
191
+ this .environment = applicationContext .getEnvironment ();
175
192
this .projectionFactory .setBeanFactory (applicationContext );
176
193
}
177
194
195
+ @ Override
196
+ public void setEnvironment (Environment environment ) {
197
+ this .environment = environment ;
198
+ }
199
+
200
+ @ Override
201
+ public Environment getEnvironment () {
202
+
203
+ if (this .environment == null ) {
204
+ this .environment = new StandardEnvironment ();
205
+ }
206
+ return this .environment ;
207
+ }
208
+
209
+ public void setSpELContext (SpELContext spELContext ) {
210
+ this .spELContext = spELContext ;
211
+ }
212
+
178
213
@ Override
179
214
public void setBeanClassLoader (ClassLoader classLoader ) {
180
215
this .beanClassLoader = classLoader ;
@@ -294,8 +329,7 @@ public <R> R project(EntityProjection<R, ?> projection, Row row) {
294
329
this ::doReadTupleValue , this ::doReadUdtValue , this ::readCollectionOrArray , this ::readMap ,
295
330
this ::getPotentiallyConvertedSimpleRead , projection );
296
331
297
- return doReadProjection (context , new RowValueProvider (row , new DefaultSpELExpressionEvaluator (row , spELContext )),
298
- projection );
332
+ return doReadProjection (context , new RowValueProvider (row , expressionEvaluatorFactory .create (row )), projection );
299
333
}
300
334
301
335
@ SuppressWarnings ("unchecked" )
@@ -334,11 +368,10 @@ private <R> R doReadProjection(ConversionContext context, CassandraValueProvider
334
368
335
369
ParameterValueProvider <CassandraPersistentProperty > provider ;
336
370
if (persistenceCreator != null && persistenceCreator .hasParameters ()) {
337
- SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator (valueProviderToUse .getSource (),
338
- spELContext );
371
+ ValueExpressionEvaluator evaluator = expressionEvaluatorFactory .create (valueProviderToUse .getSource ());
339
372
ParameterValueProvider <CassandraPersistentProperty > parameterValueProvider = newParameterValueProvider (context ,
340
373
entity , valueProviderToUse );
341
- provider = new ConverterAwareSpELExpressionParameterValueProvider (evaluator , getConversionService (),
374
+ provider = new ConverterAwareValueExpressionParameterValueProvider (evaluator , getConversionService (),
342
375
parameterValueProvider , context );
343
376
} else {
344
377
provider = NoOpParameterValueProvider .INSTANCE ;
@@ -362,8 +395,7 @@ private Object doReadOrProject(ConversionContext context, Row row, TypeInformati
362
395
363
396
if (typeDescriptor .isProjection ()) {
364
397
365
- CassandraValueProvider valueProvider = new RowValueProvider (row ,
366
- new DefaultSpELExpressionEvaluator (row , this .spELContext ));
398
+ CassandraValueProvider valueProvider = new RowValueProvider (row , expressionEvaluatorFactory .create (row ));
367
399
return doReadProjection (context , valueProvider , typeDescriptor );
368
400
}
369
401
@@ -376,7 +408,7 @@ private Object doReadOrProject(ConversionContext context, UdtValue udtValue, Typ
376
408
if (typeDescriptor .isProjection ()) {
377
409
378
410
CassandraValueProvider valueProvider = new UdtValueProvider (udtValue ,
379
- new DefaultSpELExpressionEvaluator (udtValue , this . spELContext ));
411
+ expressionEvaluatorFactory . create (udtValue ));
380
412
return doReadProjection (context , valueProvider , typeDescriptor );
381
413
}
382
414
@@ -389,7 +421,7 @@ private Object doReadOrProject(ConversionContext context, TupleValue tupleValue,
389
421
if (typeDescriptor .isProjection ()) {
390
422
391
423
CassandraValueProvider valueProvider = new TupleValueProvider (tupleValue ,
392
- new DefaultSpELExpressionEvaluator (tupleValue , this . spELContext ));
424
+ expressionEvaluatorFactory . create (tupleValue ));
393
425
return doReadProjection (context , valueProvider , typeDescriptor );
394
426
}
395
427
@@ -436,10 +468,10 @@ <S> S doReadUdtValue(ConversionContext context, UdtValue udtValue, TypeInformati
436
468
}
437
469
438
470
private <S > S doReadEntity (ConversionContext context , Object value ,
439
- Function <SpELExpressionEvaluator , CassandraValueProvider > valueProviderSupplier ,
471
+ Function <ValueExpressionEvaluator , CassandraValueProvider > valueProviderSupplier ,
440
472
TypeInformation <? extends S > typeHint ) {
441
473
442
- SpELExpressionEvaluator expressionEvaluator = new DefaultSpELExpressionEvaluator (value , this . spELContext );
474
+ ValueExpressionEvaluator expressionEvaluator = expressionEvaluatorFactory . create (value );
443
475
CassandraValueProvider valueProvider = valueProviderSupplier .apply (expressionEvaluator );
444
476
445
477
return doReadEntity (context , valueProvider , typeHint );
@@ -507,10 +539,10 @@ private <S> S doReadEntity(ConversionContext context, CassandraValueProvider val
507
539
ParameterValueProvider <CassandraPersistentProperty > provider ;
508
540
509
541
if (persistenceCreator != null && persistenceCreator .hasParameters ()) {
510
- SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator (valueProvider .getSource (), spELContext );
542
+ ValueExpressionEvaluator evaluator = expressionEvaluatorFactory . create (valueProvider .getSource ());
511
543
ParameterValueProvider <CassandraPersistentProperty > parameterValueProvider = newParameterValueProvider (context ,
512
544
entity , valueProvider );
513
- provider = new ConverterAwareSpELExpressionParameterValueProvider (evaluator , getConversionService (),
545
+ provider = new ConverterAwareValueExpressionParameterValueProvider (evaluator , getConversionService (),
514
546
parameterValueProvider , context );
515
547
} else {
516
548
provider = NoOpParameterValueProvider .INSTANCE ;
@@ -1261,23 +1293,23 @@ public <T> T getParameterValue(Parameter<T, CassandraPersistentProperty> paramet
1261
1293
}
1262
1294
1263
1295
/**
1264
- * Extension of {@link SpELExpressionParameterValueProvider } to recursively trigger value conversion on the raw
1296
+ * Extension of {@link ValueExpressionParameterValueProvider } to recursively trigger value conversion on the raw
1265
1297
* resolved SpEL value.
1266
1298
*/
1267
- private static class ConverterAwareSpELExpressionParameterValueProvider
1268
- extends SpELExpressionParameterValueProvider <CassandraPersistentProperty > {
1299
+ private static class ConverterAwareValueExpressionParameterValueProvider
1300
+ extends ValueExpressionParameterValueProvider <CassandraPersistentProperty > {
1269
1301
1270
1302
private final ConversionContext context ;
1271
1303
1272
1304
/**
1273
- * Creates a new {@link ConverterAwareSpELExpressionParameterValueProvider }.
1305
+ * Creates a new {@link ConverterAwareValueExpressionParameterValueProvider }.
1274
1306
*
1275
1307
* @param evaluator must not be {@literal null}.
1276
1308
* @param conversionService must not be {@literal null}.
1277
1309
* @param delegate must not be {@literal null}.
1278
1310
* @param context must not be {@literal null}.
1279
1311
*/
1280
- public ConverterAwareSpELExpressionParameterValueProvider ( SpELExpressionEvaluator evaluator ,
1312
+ public ConverterAwareValueExpressionParameterValueProvider ( ValueExpressionEvaluator evaluator ,
1281
1313
ConversionService conversionService , ParameterValueProvider <CassandraPersistentProperty > delegate ,
1282
1314
ConversionContext context ) {
1283
1315
@@ -1286,8 +1318,9 @@ public ConverterAwareSpELExpressionParameterValueProvider(SpELExpressionEvaluato
1286
1318
}
1287
1319
1288
1320
@ Override
1289
- protected <T > T potentiallyConvertSpelValue (Object object , Parameter <T , CassandraPersistentProperty > parameter ) {
1290
- return (T ) context .convert (object , parameter .getType ());
1321
+ protected <T > T potentiallyConvertExpressionValue (Object object ,
1322
+ Parameter <T , CassandraPersistentProperty > parameter ) {
1323
+ return context .convert (object , parameter .getType ());
1291
1324
}
1292
1325
}
1293
1326
0 commit comments