|
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; |
| 67 | +import org.springframework.core.Ordered; |
64 | 68 | import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
65 | 69 | import org.springframework.core.env.CommandLinePropertySource; |
66 | 70 | import org.springframework.core.env.CompositePropertySource; |
@@ -401,6 +405,7 @@ private void prepareContext(DefaultBootstrapContext bootstrapContext, Configurab |
401 | 405 | if (this.lazyInitialization) { |
402 | 406 | context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor()); |
403 | 407 | } |
| 408 | + context.addBeanFactoryPostProcessor(new PropertySourceOrderingBeanFactoryPostProcessor(context)); |
404 | 409 | // Load the sources |
405 | 410 | Set<Object> sources = getAllSources(); |
406 | 411 | Assert.notEmpty(sources, "Sources must not be empty"); |
@@ -1377,4 +1382,28 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) { |
1377 | 1382 | return new LinkedHashSet<>(list); |
1378 | 1383 | } |
1379 | 1384 |
|
| 1385 | + /** |
| 1386 | + * {@link BeanFactoryPostProcessor} to re-order our property sources below any |
| 1387 | + * {@code @PropertySource} items added by the {@link ConfigurationClassPostProcessor}. |
| 1388 | + */ |
| 1389 | + private static class PropertySourceOrderingBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered { |
| 1390 | + |
| 1391 | + private final ConfigurableApplicationContext context; |
| 1392 | + |
| 1393 | + PropertySourceOrderingBeanFactoryPostProcessor(ConfigurableApplicationContext context) { |
| 1394 | + this.context = context; |
| 1395 | + } |
| 1396 | + |
| 1397 | + @Override |
| 1398 | + public int getOrder() { |
| 1399 | + return Ordered.HIGHEST_PRECEDENCE; |
| 1400 | + } |
| 1401 | + |
| 1402 | + @Override |
| 1403 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 1404 | + DefaultPropertiesPropertySource.moveToEnd(this.context.getEnvironment()); |
| 1405 | + } |
| 1406 | + |
| 1407 | + } |
| 1408 | + |
1380 | 1409 | } |
0 commit comments