DATAKV-268 - Add SpEL support for @KeySpace.

We now support SpEL expressions in @KeySpace that gets evaluated on a per-operation basis.

@KeySpace("#{myProperty}")
class Person {}

Original Pull Request: #46
This commit is contained in:
Mark Paluch
2019-06-19 10:34:05 +02:00
committed by Christoph Strobl
parent 7d7021e951
commit e9128743b6
3 changed files with 119 additions and 11 deletions

View File

@@ -26,7 +26,8 @@ import org.springframework.data.annotation.Persistent;
/**
* Marker interface for methods with {@link Persistent} annotations indicating the presence of a dedicated keyspace the
* entity should reside in. If present the value will be picked up for resolving the keyspace.
* entity should reside in. If present the value will be picked up for resolving the keyspace. The {@link #value()}
* attribute supports SpEL expressions to dynamically resolve the keyspace based on a per-operation basis.
*
* <pre>
* <code>

View File

@@ -17,6 +17,10 @@ package org.springframework.data.keyvalue.core.mapping;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
@@ -31,32 +35,52 @@ import org.springframework.util.StringUtils;
public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProperty<P>>
extends BasicPersistentEntity<T, P> implements KeyValuePersistentEntity<T, P> {
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
private static final KeySpaceResolver DEFAULT_FALLBACK_RESOLVER = ClassNameKeySpaceResolver.INSTANCE;
private final @Nullable Expression keyspaceExpression;
private final @Nullable String keyspace;
/**
* @param information must not be {@literal null}.
* @param keySpaceResolver can be {@literal null}.
* @param fallbackKeySpaceResolver can be {@literal null}.
*/
public BasicKeyValuePersistentEntity(TypeInformation<T> information,
@Nullable KeySpaceResolver fallbackKeySpaceResolver) {
super(information);
this.keyspace = detectKeySpace(information.getType(), fallbackKeySpaceResolver);
}
@Nullable
private static String detectKeySpace(Class<?> type, @Nullable KeySpaceResolver fallback) {
Class<T> type = information.getType();
String keySpace = AnnotationBasedKeySpaceResolver.INSTANCE.resolveKeySpace(type);
if (StringUtils.hasText(keySpace)) {
return keySpace;
this.keyspace = keySpace;
this.keyspaceExpression = detectExpression(keySpace);
} else {
this.keyspace = resolveKeyspace(fallbackKeySpaceResolver, type);
this.keyspaceExpression = null;
}
}
return (fallback == null ? DEFAULT_FALLBACK_RESOLVER : fallback).resolveKeySpace(type);
/**
* Returns a SpEL {@link Expression} if the given {@link String} is actually an expression that does not evaluate to a
* {@link LiteralExpression} (indicating that no subsequent evaluation is necessary).
*
* @param potentialExpression can be {@literal null}
* @return
*/
@Nullable
private static Expression detectExpression(String potentialExpression) {
Expression expression = PARSER.parseExpression(potentialExpression, ParserContext.TEMPLATE_EXPRESSION);
return expression instanceof LiteralExpression ? null : expression;
}
@Nullable
private static String resolveKeyspace(@Nullable KeySpaceResolver fallbackKeySpaceResolver, Class<?> type) {
return (fallbackKeySpaceResolver == null ? DEFAULT_FALLBACK_RESOLVER : fallbackKeySpaceResolver)
.resolveKeySpace(type);
}
/*
@@ -65,6 +89,8 @@ public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProper
*/
@Override
public String getKeySpace() {
return this.keyspace;
return keyspaceExpression == null //
? keyspace //
: keyspaceExpression.getValue(getEvaluationContext(null), String.class);
}
}