Skip to content

Commit fd75f18

Browse files
author
Tim Sazon
committed
Use PropertyResolver to resolve placeholders in KeySpace
Related tickets spring-projects/spring-data-commons#2369
1 parent dc5887f commit fd75f18

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/main/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntity.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.keyvalue.core.mapping;
1717

18+
import org.springframework.core.env.PropertyResolver;
1819
import org.springframework.data.mapping.model.BasicPersistentEntity;
1920
import org.springframework.data.util.TypeInformation;
2021
import org.springframework.expression.Expression;
@@ -30,6 +31,7 @@
3031
* @author Christoph Strobl
3132
* @author Oliver Gierke
3233
* @author Mark Paluch
34+
* @author Timur Sazon
3335
* @param <T>
3436
*/
3537
public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProperty<P>>
@@ -91,8 +93,17 @@ private static String resolveKeyspace(@Nullable KeySpaceResolver fallbackKeySpac
9193
*/
9294
@Override
9395
public String getKeySpace() {
94-
return keyspaceExpression == null //
96+
String keySpace = keyspaceExpression == null //
9597
? keyspace //
9698
: keyspaceExpression.getValue(getEvaluationContext(null), String.class);
99+
100+
if (keySpace != null) {
101+
PropertyResolver propertyResolver = getPropertyResolver();
102+
if (propertyResolver != null) {
103+
return propertyResolver.resolvePlaceholders(keySpace);
104+
}
105+
}
106+
107+
return keySpace;
97108
}
98109
}

src/test/java/org/springframework/data/keyvalue/core/mapping/BasicKeyValuePersistentEntityUnitTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import org.springframework.data.mapping.context.MappingContext;
2828
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider;
2929
import org.springframework.data.spel.spi.EvaluationContextExtension;
30+
import org.springframework.mock.env.MockEnvironment;
3031

3132
/**
3233
* Unit tests for {@link BasicKeyValuePersistentEntity}.
3334
*
3435
* @author Mark Paluch
36+
* @author Timur Sazon
3537
*/
3638
class BasicKeyValuePersistentEntityUnitTests {
3739

@@ -64,6 +66,19 @@ void shouldEvaluateEntityWithoutKeyspace() {
6466
assertThat(persistentEntity.getKeySpace()).isEqualTo(NoKeyspaceEntity.class.getName());
6567
}
6668

69+
@Test
70+
void shouldEvaluateKeyspaceWithPropertyPlaceholders() {
71+
72+
BasicKeyValuePersistentEntity<?, ?> persistentEntity = (BasicKeyValuePersistentEntity<?, ?>) mappingContext
73+
.getPersistentEntity(PropertyEntity.class);
74+
persistentEntity.setPropertyResolver(new MockEnvironment().withProperty("test.property", "myValue"));
75+
76+
assertThat(persistentEntity.getKeySpace()).isEqualTo("myValue");
77+
}
78+
79+
@KeySpace("${test.property}")
80+
private static class PropertyEntity {}
81+
6782
@KeySpace("#{myProperty}")
6883
private static class ExpressionEntity {}
6984

0 commit comments

Comments
 (0)