|
33 | 33 | import org.apache.commons.logging.LogFactory;
|
34 | 34 |
|
35 | 35 | import org.springframework.beans.BeanUtils;
|
| 36 | +import org.springframework.beans.BeansException; |
36 | 37 | import org.springframework.beans.CachedIntrospectionResults;
|
37 | 38 | import org.springframework.beans.factory.config.BeanDefinition;
|
| 39 | +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; |
38 | 40 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
39 | 41 | import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
|
40 | 42 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
56 | 58 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
57 | 59 | import org.springframework.context.annotation.AnnotationConfigUtils;
|
58 | 60 | import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
| 61 | +import org.springframework.context.annotation.ConfigurationClassPostProcessor; |
59 | 62 | import org.springframework.context.support.AbstractApplicationContext;
|
60 | 63 | import org.springframework.context.support.GenericApplicationContext;
|
61 | 64 | import org.springframework.core.GenericTypeResolver;
|
| 65 | +import org.springframework.core.Ordered; |
62 | 66 | import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
63 | 67 | import org.springframework.core.env.CommandLinePropertySource;
|
64 | 68 | import org.springframework.core.env.CompositePropertySource;
|
@@ -434,6 +438,7 @@ private void prepareContext(DefaultBootstrapContext bootstrapContext, Configurab
|
434 | 438 | if (this.lazyInitialization) {
|
435 | 439 | context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
|
436 | 440 | }
|
| 441 | + context.addBeanFactoryPostProcessor(new PropertySourceOrderingBeanFactoryPostProcessor(context)); |
437 | 442 | // Load the sources
|
438 | 443 | Set<Object> sources = getAllSources();
|
439 | 444 | Assert.notEmpty(sources, "Sources must not be empty");
|
@@ -1430,4 +1435,28 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) {
|
1430 | 1435 | return new LinkedHashSet<>(list);
|
1431 | 1436 | }
|
1432 | 1437 |
|
| 1438 | + /** |
| 1439 | + * {@link BeanFactoryPostProcessor} to re-order our property sources below any |
| 1440 | + * {@code @PropertySource} items added by the {@link ConfigurationClassPostProcessor}. |
| 1441 | + */ |
| 1442 | + private static class PropertySourceOrderingBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered { |
| 1443 | + |
| 1444 | + private final ConfigurableApplicationContext context; |
| 1445 | + |
| 1446 | + PropertySourceOrderingBeanFactoryPostProcessor(ConfigurableApplicationContext context) { |
| 1447 | + this.context = context; |
| 1448 | + } |
| 1449 | + |
| 1450 | + @Override |
| 1451 | + public int getOrder() { |
| 1452 | + return Ordered.HIGHEST_PRECEDENCE; |
| 1453 | + } |
| 1454 | + |
| 1455 | + @Override |
| 1456 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 1457 | + DefaultPropertiesPropertySource.moveToEnd(this.context.getEnvironment()); |
| 1458 | + } |
| 1459 | + |
| 1460 | + } |
| 1461 | + |
1433 | 1462 | }
|
0 commit comments