26
26
import org .springframework .beans .factory .support .RootBeanDefinition ;
27
27
import org .springframework .context .ConfigurableApplicationContext ;
28
28
import org .springframework .core .env .ConfigurableEnvironment ;
29
- import org .springframework .core .env .PropertySource ;
29
+ import org .springframework .core .env .MutablePropertySources ;
30
30
import org .springframework .lang .Nullable ;
31
31
import org .springframework .test .context .ContextCustomizer ;
32
32
import org .springframework .test .context .DynamicPropertyRegistry ;
@@ -75,33 +75,35 @@ private void assertValid(Method method) {
75
75
76
76
@ Override
77
77
public void customizeContext (ConfigurableApplicationContext context , MergedContextConfiguration mergedConfig ) {
78
- DynamicValuesPropertySource propertySource = getOrAdd (context .getEnvironment ());
79
-
80
- if (!context .containsBean (DYNAMIC_PROPERTY_REGISTRY_BEAN_NAME )) {
81
- ConfigurableListableBeanFactory beanFactory = context .getBeanFactory ();
82
- beanFactory .registerSingleton (DYNAMIC_PROPERTY_REGISTRY_BEAN_NAME , propertySource .dynamicPropertyRegistry );
78
+ ConfigurableEnvironment environment = context .getEnvironment ();
79
+ ConfigurableListableBeanFactory beanFactory = context .getBeanFactory ();
80
+ if (!(beanFactory instanceof BeanDefinitionRegistry beanDefinitionRegistry )) {
81
+ throw new IllegalStateException ("BeanFactory must be a BeanDefinitionRegistry" );
83
82
}
84
83
85
- if (!context .containsBean (DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME )) {
86
- if (!(context .getBeanFactory () instanceof BeanDefinitionRegistry registry )) {
87
- throw new IllegalStateException ("BeanFactory must be a BeanDefinitionRegistry" );
88
- }
89
- BeanDefinition beanDefinition = new RootBeanDefinition (DynamicPropertySourceBeanInitializer .class );
90
- beanDefinition .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
91
- registry .registerBeanDefinition (DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME , beanDefinition );
84
+ DefaultDynamicPropertyRegistry dynamicPropertyRegistry =
85
+ new DefaultDynamicPropertyRegistry (environment , this .methods .isEmpty ());
86
+ beanFactory .registerSingleton (DYNAMIC_PROPERTY_REGISTRY_BEAN_NAME , dynamicPropertyRegistry );
87
+
88
+ BeanDefinition beanDefinition = new RootBeanDefinition (DynamicPropertySourceBeanInitializer .class );
89
+ beanDefinition .setRole (BeanDefinition .ROLE_INFRASTRUCTURE );
90
+ beanDefinitionRegistry .registerBeanDefinition (
91
+ DYNAMIC_PROPERTY_SOURCE_BEAN_INITIALIZER_BEAN_NAME , beanDefinition );
92
+
93
+ if (!this .methods .isEmpty ()) {
94
+ MutablePropertySources propertySources = environment .getPropertySources ();
95
+ propertySources .addFirst (new DynamicValuesPropertySource (dynamicPropertyRegistry .valueSuppliers ));
96
+ this .methods .forEach (method -> {
97
+ ReflectionUtils .makeAccessible (method );
98
+ ReflectionUtils .invokeMethod (method , null , dynamicPropertyRegistry );
99
+ });
92
100
}
93
-
94
- this .methods .forEach (method -> {
95
- ReflectionUtils .makeAccessible (method );
96
- ReflectionUtils .invokeMethod (method , null , propertySource .dynamicPropertyRegistry );
97
- });
98
101
}
99
102
100
103
Set <Method > getMethods () {
101
104
return this .methods ;
102
105
}
103
106
104
-
105
107
@ Override
106
108
public boolean equals (@ Nullable Object other ) {
107
109
return (this == other || (other instanceof DynamicPropertiesContextCustomizer that &&
@@ -113,17 +115,4 @@ public int hashCode() {
113
115
return this .methods .hashCode ();
114
116
}
115
117
116
-
117
- private static DynamicValuesPropertySource getOrAdd (ConfigurableEnvironment environment ) {
118
- PropertySource <?> propertySource = environment .getPropertySources ()
119
- .get (DynamicValuesPropertySource .PROPERTY_SOURCE_NAME );
120
- if (propertySource == null ) {
121
- environment .getPropertySources ().addFirst (new DynamicValuesPropertySource ());
122
- return getOrAdd (environment );
123
- }
124
- Assert .state (propertySource instanceof DynamicValuesPropertySource ,
125
- "Incorrect DynamicValuesPropertySource type registered" );
126
- return (DynamicValuesPropertySource ) propertySource ;
127
- }
128
-
129
118
}
0 commit comments