From e4e06e546dbe0bb3d9f5f28ef4670d36bdb355d0 Mon Sep 17 00:00:00 2001 From: Tim Sazon Date: Thu, 6 May 2021 18:46:11 +0300 Subject: [PATCH] Add PropertyResolver to BasicPersistentEntity for replacing placeholders in the entity values Closes #2369 --- .../context/AbstractMappingContext.java | 8 ++++++++ .../mapping/model/BasicPersistentEntity.java | 20 +++++++++++++++++++ .../model/MutablePersistentEntity.java | 10 ++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index 63f01a3552..4b2c53fe41 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -43,6 +43,7 @@ import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.core.KotlinDetector; import org.springframework.core.NativeDetector; +import org.springframework.core.env.PropertyResolver; import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; @@ -87,6 +88,7 @@ * @author Mark Paluch * @author Mikael Klamra * @author Christoph Strobl + * @author Tim Sazon */ public abstract class AbstractMappingContext, P extends PersistentProperty

> implements MappingContext, ApplicationEventPublisherAware, ApplicationContextAware, InitializingBean { @@ -99,6 +101,7 @@ public abstract class AbstractMappingContext persistentPropertyPathFactory; private @Nullable ApplicationEventPublisher applicationEventPublisher; + private @Nullable PropertyResolver propertyResolver; private EvaluationContextProvider evaluationContextProvider = EvaluationContextProvider.DEFAULT; private Set> initialEntitySet = new HashSet<>(); @@ -142,6 +145,10 @@ public void setApplicationContext(ApplicationContext applicationContext) throws if (applicationEventPublisher == null) { this.applicationEventPublisher = applicationContext; } + + if (propertyResolver == null) { + this.propertyResolver = applicationContext.getEnvironment(); + } } /** @@ -373,6 +380,7 @@ protected Optional addPersistentEntity(TypeInformation typeInformation) { entity = createPersistentEntity(typeInformation); entity.setEvaluationContextProvider(evaluationContextProvider); + entity.setPropertyResolver(propertyResolver); // Eagerly cache the entity as we might have to find it during recursive lookups. persistentEntities.put(typeInformation, Optional.of(entity)); diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index cd47b6a777..5d20fa468e 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -30,6 +30,7 @@ import java.util.stream.Collectors; import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.core.env.PropertyResolver; import org.springframework.data.annotation.Immutable; import org.springframework.data.annotation.TypeAlias; import org.springframework.data.domain.Persistable; @@ -58,6 +59,7 @@ * @author Thomas Darimont * @author Christoph Strobl * @author Mark Paluch + * @author Tim Sazon */ public class BasicPersistentEntity> implements MutablePersistentEntity { @@ -78,6 +80,7 @@ public class BasicPersistentEntity> implement private @Nullable P versionProperty; private PersistentPropertyAccessorFactory propertyAccessorFactory; private EvaluationContextProvider evaluationContextProvider = EvaluationContextProvider.DEFAULT; + private @Nullable PropertyResolver propertyResolver; private final Lazy typeAlias; private final Lazy isNewStrategy; @@ -254,6 +257,15 @@ public void setEvaluationContextProvider(EvaluationContextProvider provider) { this.evaluationContextProvider = provider; } + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.model.MutablePersistentEntity#setPropertyResolver(org.springframework.core.env.PropertyResolver) + */ + @Override + public void setPropertyResolver(@Nullable PropertyResolver propertyResolver) { + this.propertyResolver = propertyResolver; + } + /** * Returns the given property if it is a better candidate for the id property than the current id property. * @@ -561,6 +573,14 @@ protected EvaluationContext getEvaluationContext(Object rootObject, ExpressionDe return evaluationContextProvider.getEvaluationContext(rootObject, dependencies); } + /** + * Returns the {@link PropertyResolver}. + */ + @Nullable + protected PropertyResolver getPropertyResolver() { + return propertyResolver; + } + /** * Returns the default {@link IsNewStrategy} to be used. Will be a {@link PersistentEntityIsNewStrategy} by default. * Note, that this strategy only gets used if the entity doesn't implement {@link Persistable} as this indicates the diff --git a/src/main/java/org/springframework/data/mapping/model/MutablePersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/MutablePersistentEntity.java index 3e4316ce3b..d71af52c2c 100644 --- a/src/main/java/org/springframework/data/mapping/model/MutablePersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/MutablePersistentEntity.java @@ -15,18 +15,21 @@ */ package org.springframework.data.mapping.model; +import org.springframework.core.env.PropertyResolver; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.spel.EvaluationContextProvider; +import org.springframework.lang.Nullable; /** * Interface capturing mutator methods for {@link PersistentEntity}s. * * @author Oliver Gierke * @author Mark Paluch + * @author Tim Sazon */ public interface MutablePersistentEntity> extends PersistentEntity { @@ -66,4 +69,11 @@ public interface MutablePersistentEntity> ext * @param provider must not be {@literal null}. */ void setEvaluationContextProvider(EvaluationContextProvider provider); + + /** + * Configures the {@link PropertyResolver} to be used by the entity. + * + * @param propertyResolver can be {@literal null}. + */ + void setPropertyResolver(@Nullable PropertyResolver propertyResolver); }