Skip to content

Commit fc6a00e

Browse files
author
Tim Sazon
committed
Add PropertyResolver to BasicPersistentEntity for replacing placeholders in the entity values
Closes #2369
1 parent ac9b127 commit fc6a00e

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.context.ApplicationEventPublisherAware;
4444
import org.springframework.core.KotlinDetector;
4545
import org.springframework.core.NativeDetector;
46+
import org.springframework.core.env.PropertyResolver;
4647
import org.springframework.data.mapping.MappingException;
4748
import org.springframework.data.mapping.PersistentEntity;
4849
import org.springframework.data.mapping.PersistentProperty;
@@ -87,6 +88,7 @@
8788
* @author Mark Paluch
8889
* @author Mikael Klamra
8990
* @author Christoph Strobl
91+
* @author Tim Sazon
9092
*/
9193
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
9294
implements MappingContext<E, P>, ApplicationEventPublisherAware, ApplicationContextAware, InitializingBean {
@@ -99,6 +101,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
99101
private final PersistentPropertyPathFactory<E, P> persistentPropertyPathFactory;
100102

101103
private @Nullable ApplicationEventPublisher applicationEventPublisher;
104+
private @Nullable PropertyResolver propertyResolver;
102105
private EvaluationContextProvider evaluationContextProvider = EvaluationContextProvider.DEFAULT;
103106

104107
private Set<? extends Class<?>> initialEntitySet = new HashSet<>();
@@ -142,6 +145,10 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
142145
if (applicationEventPublisher == null) {
143146
this.applicationEventPublisher = applicationContext;
144147
}
148+
149+
if (propertyResolver == null) {
150+
this.propertyResolver = applicationContext.getEnvironment();
151+
}
145152
}
146153

147154
/**
@@ -373,6 +380,7 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
373380
entity = createPersistentEntity(typeInformation);
374381

375382
entity.setEvaluationContextProvider(evaluationContextProvider);
383+
entity.setPropertyResolver(propertyResolver);
376384

377385
// Eagerly cache the entity as we might have to find it during recursive lookups.
378386
persistentEntities.put(typeInformation, Optional.of(entity));

src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

+20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.stream.Collectors;
3131

3232
import org.springframework.core.annotation.AnnotatedElementUtils;
33+
import org.springframework.core.env.PropertyResolver;
3334
import org.springframework.data.annotation.Immutable;
3435
import org.springframework.data.annotation.TypeAlias;
3536
import org.springframework.data.domain.Persistable;
@@ -58,6 +59,7 @@
5859
* @author Thomas Darimont
5960
* @author Christoph Strobl
6061
* @author Mark Paluch
62+
* @author Tim Sazon
6163
*/
6264
public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implements MutablePersistentEntity<T, P> {
6365

@@ -78,6 +80,7 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
7880
private @Nullable P versionProperty;
7981
private PersistentPropertyAccessorFactory propertyAccessorFactory;
8082
private EvaluationContextProvider evaluationContextProvider = EvaluationContextProvider.DEFAULT;
83+
private @Nullable PropertyResolver propertyResolver;
8184

8285
private final Lazy<Alias> typeAlias;
8386
private final Lazy<IsNewStrategy> isNewStrategy;
@@ -254,6 +257,15 @@ public void setEvaluationContextProvider(EvaluationContextProvider provider) {
254257
this.evaluationContextProvider = provider;
255258
}
256259

260+
/*
261+
* (non-Javadoc)
262+
* @see org.springframework.data.mapping.model.MutablePersistentEntity#setPropertyResolver(org.springframework.core.env.PropertyResolver)
263+
*/
264+
@Override
265+
public void setPropertyResolver(@Nullable PropertyResolver propertyResolver) {
266+
this.propertyResolver = propertyResolver;
267+
}
268+
257269
/**
258270
* Returns the given property if it is a better candidate for the id property than the current id property.
259271
*
@@ -561,6 +573,14 @@ protected EvaluationContext getEvaluationContext(Object rootObject, ExpressionDe
561573
return evaluationContextProvider.getEvaluationContext(rootObject, dependencies);
562574
}
563575

576+
/**
577+
* Returns the {@link PropertyResolver}.
578+
*/
579+
@Nullable
580+
protected PropertyResolver getPropertyResolver() {
581+
return propertyResolver;
582+
}
583+
564584
/**
565585
* Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default.
566586
* Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the

src/main/java/org/springframework/data/mapping/model/MutablePersistentEntity.java

+10
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
*/
1616
package org.springframework.data.mapping.model;
1717

18+
import org.springframework.core.env.PropertyResolver;
1819
import org.springframework.data.mapping.Association;
1920
import org.springframework.data.mapping.MappingException;
2021
import org.springframework.data.mapping.PersistentEntity;
2122
import org.springframework.data.mapping.PersistentProperty;
2223
import org.springframework.data.mapping.PersistentPropertyAccessor;
2324
import org.springframework.data.spel.EvaluationContextProvider;
25+
import org.springframework.lang.Nullable;
2426

2527
/**
2628
* Interface capturing mutator methods for {@link PersistentEntity}s.
2729
*
2830
* @author Oliver Gierke
2931
* @author Mark Paluch
32+
* @author Tim Sazon
3033
*/
3134
public interface MutablePersistentEntity<T, P extends PersistentProperty<P>> extends PersistentEntity<T, P> {
3235

@@ -66,4 +69,11 @@ public interface MutablePersistentEntity<T, P extends PersistentProperty<P>> ext
6669
* @param provider must not be {@literal null}.
6770
*/
6871
void setEvaluationContextProvider(EvaluationContextProvider provider);
72+
73+
/**
74+
* Configures the {@link PropertyResolver} to be used by the entity.
75+
*
76+
* @param propertyResolver can be {@literal null}.
77+
*/
78+
void setPropertyResolver(@Nullable PropertyResolver propertyResolver);
6979
}

0 commit comments

Comments
 (0)