|
34 | 34 | import org.apache.commons.logging.LogFactory; |
35 | 35 |
|
36 | 36 | import org.springframework.beans.BeanUtils; |
| 37 | +import org.springframework.beans.BeansException; |
37 | 38 | import org.springframework.beans.CachedIntrospectionResults; |
38 | 39 | import org.springframework.beans.factory.config.BeanDefinition; |
| 40 | +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
39 | 41 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
40 | 42 | import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader; |
41 | 43 | import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory; |
|
58 | 60 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
59 | 61 | import org.springframework.context.annotation.AnnotationConfigUtils; |
60 | 62 | import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; |
| 63 | +import org.springframework.context.annotation.ConfigurationClassPostProcessor; |
61 | 64 | import org.springframework.context.support.AbstractApplicationContext; |
62 | 65 | import org.springframework.context.support.GenericApplicationContext; |
63 | 66 | import org.springframework.core.GenericTypeResolver; |
@@ -390,6 +393,7 @@ private void prepareContext(DefaultBootstrapContext bootstrapContext, Configurab |
390 | 393 | if (this.lazyInitialization) { |
391 | 394 | context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()); |
392 | 395 | } |
| 396 | + context.addBeanFactoryPostProcessor(new PropertySourceOrderingBeanFactoryPostProcessor(context)); |
393 | 397 | // Load the sources |
394 | 398 | Set<Object> sources = getAllSources(); |
395 | 399 | Assert.notEmpty(sources, "Sources must not be empty"); |
@@ -1368,4 +1372,28 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) { |
1368 | 1372 | return new LinkedHashSet<>(list); |
1369 | 1373 | } |
1370 | 1374 |
|
| 1375 | + /** |
| 1376 | + * {@link BeanFactoryPostProcessor} to re-order our property sources below any |
| 1377 | + * {@code @PropertySource} items added by the {@link ConfigurationClassPostProcessor}. |
| 1378 | + */ |
| 1379 | + private static class PropertySourceOrderingBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered { |
| 1380 | + |
| 1381 | + private final ConfigurableApplicationContext context; |
| 1382 | + |
| 1383 | + PropertySourceOrderingBeanFactoryPostProcessor(ConfigurableApplicationContext context) { |
| 1384 | + this.context = context; |
| 1385 | + } |
| 1386 | + |
| 1387 | + @Override |
| 1388 | + public int getOrder() { |
| 1389 | + return Ordered.HIGHEST_PRECEDENCE; |
| 1390 | + } |
| 1391 | + |
| 1392 | + @Override |
| 1393 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 1394 | + DefaultPropertiesPropertySource.moveToEnd(this.context.getEnvironment()); |
| 1395 | + } |
| 1396 | + |
| 1397 | + } |
| 1398 | + |
1371 | 1399 | } |
0 commit comments