|
24 | 24 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
25 | 25 | import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
|
26 | 26 | import org.springframework.context.EnvironmentAware;
|
27 |
| -import org.springframework.core.env.CompositePropertySource; |
28 | 27 | import org.springframework.core.env.ConfigurableEnvironment;
|
29 | 28 | import org.springframework.core.env.ConfigurablePropertyResolver;
|
30 | 29 | import org.springframework.core.env.Environment;
|
@@ -133,23 +132,10 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
133 | 132 | if (this.propertySources == null) {
|
134 | 133 | this.propertySources = new MutablePropertySources();
|
135 | 134 | if (this.environment != null) {
|
136 |
| - PropertySource<?> environmentPropertySource; |
137 |
| - if (this.environment instanceof ConfigurableEnvironment configurableEnvironment) { |
138 |
| - environmentPropertySource = new CompositePropertySource(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, |
139 |
| - configurableEnvironment.getPropertySources()); |
140 |
| - } |
141 |
| - else { |
142 |
| - // Fallback code path that should never apply in a regular scenario, since the |
143 |
| - // Environment in the ApplicationContext should always be a ConfigurableEnvironment. |
144 |
| - environmentPropertySource = |
145 |
| - new PropertySource<>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) { |
146 |
| - @Override |
147 |
| - @Nullable |
148 |
| - public Object getProperty(String key) { |
149 |
| - return super.source.getProperty(key); |
150 |
| - } |
151 |
| - }; |
152 |
| - } |
| 135 | + PropertySource<?> environmentPropertySource = |
| 136 | + (this.environment instanceof ConfigurableEnvironment configurableEnvironment ? |
| 137 | + new ConfigurableEnvironmentPropertySource(configurableEnvironment) : |
| 138 | + new FallbackEnvironmentPropertySource(this.environment)); |
153 | 139 | this.propertySources.addLast(environmentPropertySource);
|
154 | 140 | }
|
155 | 141 | try {
|
@@ -232,4 +218,75 @@ public PropertySources getAppliedPropertySources() throws IllegalStateException
|
232 | 218 | return this.appliedPropertySources;
|
233 | 219 | }
|
234 | 220 |
|
| 221 | + |
| 222 | + /** |
| 223 | + * Custom {@link PropertySource} that delegates to the |
| 224 | + * {@link ConfigurableEnvironment#getPropertySources() PropertySources} in a |
| 225 | + * {@link ConfigurableEnvironment}. |
| 226 | + * @since 6.2.7 |
| 227 | + */ |
| 228 | + private static class ConfigurableEnvironmentPropertySource extends PropertySource<ConfigurableEnvironment> { |
| 229 | + |
| 230 | + private final PropertySources propertySources; |
| 231 | + |
| 232 | + |
| 233 | + ConfigurableEnvironmentPropertySource(ConfigurableEnvironment environment) { |
| 234 | + super(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, environment); |
| 235 | + this.propertySources = environment.getPropertySources(); |
| 236 | + } |
| 237 | + |
| 238 | + |
| 239 | + @Override |
| 240 | + @Nullable |
| 241 | + public Object getProperty(String name) { |
| 242 | + for (PropertySource<?> propertySource : this.propertySources) { |
| 243 | + Object candidate = propertySource.getProperty(name); |
| 244 | + if (candidate != null) { |
| 245 | + return candidate; |
| 246 | + } |
| 247 | + } |
| 248 | + return null; |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public boolean containsProperty(String name) { |
| 253 | + for (PropertySource<?> propertySource : this.propertySources) { |
| 254 | + if (propertySource.containsProperty(name)) { |
| 255 | + return true; |
| 256 | + } |
| 257 | + } |
| 258 | + return false; |
| 259 | + } |
| 260 | + |
| 261 | + @Override |
| 262 | + public String toString() { |
| 263 | + return "ConfigurableEnvironmentPropertySource {propertySources=" + this.propertySources + "}"; |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * Fallback {@link PropertySource} that delegates to a raw {@link Environment}. |
| 269 | + * <p>Should never apply in a regular scenario, since the {@code Environment} |
| 270 | + * in an {@code ApplicationContext} should always be a {@link ConfigurableEnvironment}. |
| 271 | + * @since 6.2.7 |
| 272 | + */ |
| 273 | + private static class FallbackEnvironmentPropertySource extends PropertySource<Environment> { |
| 274 | + |
| 275 | + FallbackEnvironmentPropertySource(Environment environment) { |
| 276 | + super(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, environment); |
| 277 | + } |
| 278 | + |
| 279 | + |
| 280 | + @Override |
| 281 | + @Nullable |
| 282 | + public Object getProperty(String name) { |
| 283 | + return super.source.getProperty(name); |
| 284 | + } |
| 285 | + |
| 286 | + @Override |
| 287 | + public String toString() { |
| 288 | + return "FallbackEnvironmentPropertySource {environment=" + super.source + "}"; |
| 289 | + } |
| 290 | + } |
| 291 | + |
235 | 292 | }
|
0 commit comments