diff --git a/src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java b/src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java index 6a37e62..deadaf7 100644 --- a/src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java +++ b/src/main/java/org/springframework/data/keyvalue/annotation/KeySpace.java @@ -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. * *
  * 
diff --git a/src/main/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntity.java b/src/main/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntity.java
index f115ddc..48f0b79 100644
--- a/src/main/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntity.java
+++ b/src/main/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntity.java
@@ -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>
 		extends BasicPersistentEntity implements KeyValuePersistentEntity {
 
+	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 information,
 			@Nullable KeySpaceResolver fallbackKeySpaceResolver) {
 
 		super(information);
 
-		this.keyspace = detectKeySpace(information.getType(), fallbackKeySpaceResolver);
-	}
-
-	@Nullable
-	private static String detectKeySpace(Class type, @Nullable KeySpaceResolver fallback) {
-
+		Class 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, ? extends KeyValuePersistentProperty> mappingContext = new KeyValueMappingContext<>();
+
+	@Test // DATAKV-268
+	public void shouldDeriveKeyspaceFromClassName() {
+
+		KeyValuePersistentEntity persistentEntity = mappingContext.getPersistentEntity(KeyspaceEntity.class);
+
+		assertThat(persistentEntity.getKeySpace()).isEqualTo(KeyspaceEntity.class.getName());
+	}
+
+	@Test // DATAKV-268
+	public void shouldEvaluateKeyspaceExpression() {
+
+		KeyValuePersistentEntity persistentEntity = mappingContext.getPersistentEntity(ExpressionEntity.class);
+
+		persistentEntity.setEvaluationContextProvider(
+				new ExtensionAwareEvaluationContextProvider(Collections.singletonList(new SampleExtension())));
+
+		assertThat(persistentEntity.getKeySpace()).isEqualTo("some");
+	}
+
+	@KeySpace("#{myProperty}")
+	static class ExpressionEntity {}
+
+	@KeySpace
+	static class KeyspaceEntity {}
+
+	static class SampleExtension implements EvaluationContextExtension {
+
+		@Override
+		public String getExtensionId() {
+			return "sampleExtension";
+		}
+
+		@Override
+		public Map getProperties() {
+
+			Map properties = new LinkedHashMap<>();
+			properties.put("myProperty", "some");
+			return properties;
+		}
+	}
+}