Skip to content

Add an ability to resolve property placeholders in BasicPersistentEntity #2370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +88,7 @@
* @author Mark Paluch
* @author Mikael Klamra
* @author Christoph Strobl
* @author Tim Sazon
*/
public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?, P>, P extends PersistentProperty<P>>
implements MappingContext<E, P>, ApplicationEventPublisherAware, ApplicationContextAware, InitializingBean {
Expand All @@ -99,6 +101,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
private final PersistentPropertyPathFactory<E, P> persistentPropertyPathFactory;

private @Nullable ApplicationEventPublisher applicationEventPublisher;
private @Nullable PropertyResolver propertyResolver;
private EvaluationContextProvider evaluationContextProvider = EvaluationContextProvider.DEFAULT;

private Set<? extends Class<?>> initialEntitySet = new HashSet<>();
Expand Down Expand Up @@ -142,6 +145,10 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
if (applicationEventPublisher == null) {
this.applicationEventPublisher = applicationContext;
}

if (propertyResolver == null) {
this.propertyResolver = applicationContext.getEnvironment();
}
}

/**
Expand Down Expand Up @@ -373,6 +380,7 @@ protected Optional<E> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,7 @@
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @author Tim Sazon
*/
public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implements MutablePersistentEntity<T, P> {

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

private final Lazy<Alias> typeAlias;
private final Lazy<IsNewStrategy> isNewStrategy;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, P extends PersistentProperty<P>> extends PersistentEntity<T, P> {

Expand Down Expand Up @@ -66,4 +69,11 @@ public interface MutablePersistentEntity<T, P extends PersistentProperty<P>> 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);
}