17
17
18
18
import org .springframework .data .mapping .model .BasicPersistentEntity ;
19
19
import org .springframework .data .util .TypeInformation ;
20
+ import org .springframework .expression .Expression ;
21
+ import org .springframework .expression .ParserContext ;
22
+ import org .springframework .expression .common .LiteralExpression ;
23
+ import org .springframework .expression .spel .standard .SpelExpressionParser ;
20
24
import org .springframework .lang .Nullable ;
21
25
import org .springframework .util .StringUtils ;
22
26
31
35
public class BasicKeyValuePersistentEntity <T , P extends KeyValuePersistentProperty <P >>
32
36
extends BasicPersistentEntity <T , P > implements KeyValuePersistentEntity <T , P > {
33
37
38
+ private static final SpelExpressionParser PARSER = new SpelExpressionParser ();
39
+
34
40
private static final KeySpaceResolver DEFAULT_FALLBACK_RESOLVER = ClassNameKeySpaceResolver .INSTANCE ;
35
41
42
+ private final @ Nullable Expression keyspaceExpression ;
36
43
private final @ Nullable String keyspace ;
37
44
38
45
/**
39
46
* @param information must not be {@literal null}.
40
- * @param keySpaceResolver can be {@literal null}.
47
+ * @param fallbackKeySpaceResolver can be {@literal null}.
41
48
*/
42
49
public BasicKeyValuePersistentEntity (TypeInformation <T > information ,
43
50
@ Nullable KeySpaceResolver fallbackKeySpaceResolver ) {
44
51
45
52
super (information );
46
53
47
- this .keyspace = detectKeySpace (information .getType (), fallbackKeySpaceResolver );
48
- }
49
-
50
- @ Nullable
51
- private static String detectKeySpace (Class <?> type , @ Nullable KeySpaceResolver fallback ) {
52
-
54
+ Class <T > type = information .getType ();
53
55
String keySpace = AnnotationBasedKeySpaceResolver .INSTANCE .resolveKeySpace (type );
54
56
55
57
if (StringUtils .hasText (keySpace )) {
56
- return keySpace ;
58
+ this .keyspace = keySpace ;
59
+ this .keyspaceExpression = detectExpression (keySpace );
60
+ } else {
61
+ this .keyspace = resolveKeyspace (fallbackKeySpaceResolver , type );
62
+ this .keyspaceExpression = null ;
57
63
}
64
+ }
58
65
59
- return (fallback == null ? DEFAULT_FALLBACK_RESOLVER : fallback ).resolveKeySpace (type );
66
+ /**
67
+ * Returns a SpEL {@link Expression} if the given {@link String} is actually an expression that does not evaluate to a
68
+ * {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
69
+ *
70
+ * @param potentialExpression can be {@literal null}
71
+ * @return
72
+ */
73
+ @ Nullable
74
+ private static Expression detectExpression (String potentialExpression ) {
75
+
76
+ Expression expression = PARSER .parseExpression (potentialExpression , ParserContext .TEMPLATE_EXPRESSION );
77
+ return expression instanceof LiteralExpression ? null : expression ;
78
+ }
79
+
80
+ @ Nullable
81
+ private static String resolveKeyspace (@ Nullable KeySpaceResolver fallbackKeySpaceResolver , Class <?> type ) {
82
+ return (fallbackKeySpaceResolver == null ? DEFAULT_FALLBACK_RESOLVER : fallbackKeySpaceResolver )
83
+ .resolveKeySpace (type );
60
84
}
61
85
62
86
/*
@@ -65,6 +89,8 @@ private static String detectKeySpace(Class<?> type, @Nullable KeySpaceResolver f
65
89
*/
66
90
@ Override
67
91
public String getKeySpace () {
68
- return this .keyspace ;
92
+ return keyspaceExpression == null //
93
+ ? keyspace //
94
+ : keyspaceExpression .getValue (getEvaluationContext (null ), String .class );
69
95
}
70
96
}
0 commit comments