40
40
import org .springframework .context .ApplicationContextAware ;
41
41
import org .springframework .context .ApplicationEventPublisher ;
42
42
import org .springframework .context .ApplicationEventPublisherAware ;
43
+ import org .springframework .context .EnvironmentAware ;
43
44
import org .springframework .core .KotlinDetector ;
44
45
import org .springframework .core .NativeDetector ;
46
+ import org .springframework .core .env .Environment ;
47
+ import org .springframework .core .env .StandardEnvironment ;
45
48
import org .springframework .data .domain .ManagedTypes ;
46
49
import org .springframework .data .mapping .MappingException ;
47
50
import org .springframework .data .mapping .PersistentEntity ;
48
51
import org .springframework .data .mapping .PersistentProperty ;
49
52
import org .springframework .data .mapping .PersistentPropertyPath ;
50
53
import org .springframework .data .mapping .PersistentPropertyPaths ;
51
54
import org .springframework .data .mapping .PropertyPath ;
55
+ import org .springframework .data .mapping .model .AbstractPersistentProperty ;
52
56
import org .springframework .data .mapping .model .BeanWrapperPropertyAccessorFactory ;
53
57
import org .springframework .data .mapping .model .ClassGeneratingPropertyAccessorFactory ;
54
58
import org .springframework .data .mapping .model .EntityInstantiators ;
59
63
import org .springframework .data .mapping .model .SimpleTypeHolder ;
60
64
import org .springframework .data .spel .EvaluationContextProvider ;
61
65
import org .springframework .data .spel .ExtensionAwareEvaluationContextProvider ;
66
+ import org .springframework .data .support .EnvironmentAccessor ;
62
67
import org .springframework .data .util .KotlinReflectionUtils ;
63
68
import org .springframework .data .util .NullableWrapperConverters ;
64
69
import org .springframework .data .util .Optionals ;
89
94
* @author Christoph Strobl
90
95
*/
91
96
public abstract class AbstractMappingContext <E extends MutablePersistentEntity <?, P >, P extends PersistentProperty <P >>
92
- implements MappingContext <E , P >, ApplicationEventPublisherAware , ApplicationContextAware , InitializingBean {
97
+ implements MappingContext <E , P >, ApplicationEventPublisherAware , ApplicationContextAware , InitializingBean , EnvironmentAware {
93
98
94
99
private static final Log LOGGER = LogFactory .getLog (MappingContext .class );
95
100
@@ -100,6 +105,7 @@ public abstract class AbstractMappingContext<E extends MutablePersistentEntity<?
100
105
101
106
private @ Nullable ApplicationEventPublisher applicationEventPublisher ;
102
107
private EvaluationContextProvider evaluationContextProvider = EvaluationContextProvider .DEFAULT ;
108
+ private @ Nullable EnvironmentAccessor environmentAccessor ;
103
109
104
110
private ManagedTypes managedTypes = ManagedTypes .empty ();
105
111
@@ -138,6 +144,10 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
138
144
}
139
145
}
140
146
147
+ public void setEnvironment (Environment environment ) {
148
+ this .environmentAccessor = new DelegatingEnvironmentAccessor (environment );
149
+ }
150
+
141
151
/**
142
152
* Sets the {@link Set} of types to populate the context initially.
143
153
*
@@ -406,6 +416,7 @@ private E doAddPersistentEntity(TypeInformation<?> typeInformation) {
406
416
407
417
E entity = createPersistentEntity (userTypeInformation );
408
418
entity .setEvaluationContextProvider (evaluationContextProvider );
419
+ entity .setEnvironmentAccessor (environmentAccessor );
409
420
410
421
// Eagerly cache the entity as we might have to find it during recursive lookups.
411
422
persistentEntities .put (userTypeInformation , Optional .of (entity ));
@@ -477,6 +488,10 @@ public Collection<TypeInformation<?>> getManagedTypes() {
477
488
478
489
@ Override
479
490
public void afterPropertiesSet () {
491
+
492
+ if (this .environmentAccessor == null ) {
493
+ this .environmentAccessor = new DelegatingEnvironmentAccessor (new StandardEnvironment ());
494
+ }
480
495
initialize ();
481
496
}
482
497
@@ -579,6 +594,9 @@ private void createAndRegisterProperty(Property input) {
579
594
return ;
580
595
}
581
596
597
+ if (property instanceof AbstractPersistentProperty <?> pp ) {
598
+ pp .setEnvironmentAccessor (environmentAccessor );
599
+ }
582
600
entity .addPersistentProperty (property );
583
601
584
602
if (property .isAssociation ()) {
@@ -776,4 +794,32 @@ public boolean matches(String name, Class<?> type) {
776
794
}
777
795
}
778
796
}
797
+
798
+ /**
799
+ * @author Christoph Strobl
800
+ * @since 3.3
801
+ */
802
+ public static class DelegatingEnvironmentAccessor implements EnvironmentAccessor {
803
+
804
+ private final Environment environment ;
805
+
806
+ static EnvironmentAccessor standard () {
807
+ return new DelegatingEnvironmentAccessor (new StandardEnvironment ());
808
+ }
809
+
810
+ public DelegatingEnvironmentAccessor (Environment environment ) {
811
+ this .environment = environment ;
812
+ }
813
+
814
+ @ Nullable
815
+ @ Override
816
+ public String getProperty (String key ) {
817
+ return environment .getProperty (key );
818
+ }
819
+
820
+ @ Override
821
+ public String resolvePlaceholders (String text ) {
822
+ return environment .resolvePlaceholders (text );
823
+ }
824
+ }
779
825
}
0 commit comments