Skip to content

Commit e83ed4f

Browse files
committed
Merge branch '2.7.x'
2 parents 06f8041 + fbde59d commit e83ed4f

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import org.apache.commons.logging.Log;
3333
import org.apache.commons.logging.LogFactory;
3434

35+
import org.springframework.beans.BeansException;
3536
import org.springframework.beans.CachedIntrospectionResults;
3637
import org.springframework.beans.factory.config.BeanDefinition;
38+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
3739
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3840
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
3941
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
@@ -56,6 +58,7 @@
5658
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5759
import org.springframework.context.annotation.AnnotationConfigUtils;
5860
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
61+
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
5962
import org.springframework.context.support.AbstractApplicationContext;
6063
import org.springframework.context.support.GenericApplicationContext;
6164
import org.springframework.core.GenericTypeResolver;
@@ -397,6 +400,7 @@ private void prepareContext(DefaultBootstrapContext bootstrapContext, Configurab
397400
if (this.lazyInitialization) {
398401
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
399402
}
403+
context.addBeanFactoryPostProcessor(new PropertySourceOrderingBeanFactoryPostProcessor(context));
400404
if (!NativeDetector.inNativeImage()) {
401405
// Load the sources
402406
Set<Object> sources = getAllSources();
@@ -1374,4 +1378,28 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) {
13741378
return new LinkedHashSet<>(list);
13751379
}
13761380

1381+
/**
1382+
* {@link BeanFactoryPostProcessor} to re-order our property sources below any
1383+
* {@code @PropertySource} items added by the {@link ConfigurationClassPostProcessor}.
1384+
*/
1385+
private static class PropertySourceOrderingBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {
1386+
1387+
private final ConfigurableApplicationContext context;
1388+
1389+
PropertySourceOrderingBeanFactoryPostProcessor(ConfigurableApplicationContext context) {
1390+
this.context = context;
1391+
}
1392+
1393+
@Override
1394+
public int getOrder() {
1395+
return Ordered.HIGHEST_PRECEDENCE;
1396+
}
1397+
1398+
@Override
1399+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
1400+
DefaultPropertiesPropertySource.moveToEnd(this.context.getEnvironment());
1401+
}
1402+
1403+
}
1404+
13771405
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,16 @@ void bindsEnvironmentPrefixToSpringApplication() {
12501250
assertThat(application.getEnvironmentPrefix()).isEqualTo("my");
12511251
}
12521252

1253+
@Test
1254+
void movesConfigClassPropertySourcesToEnd() {
1255+
SpringApplication application = new SpringApplication(PropertySourceConfig.class);
1256+
application.setWebApplicationType(WebApplicationType.NONE);
1257+
application.setDefaultProperties(Collections.singletonMap("test.name", "test"));
1258+
this.context = application.run();
1259+
assertThat(this.context.getEnvironment().getProperty("test.name"))
1260+
.isEqualTo("spring-application-config-property-source");
1261+
}
1262+
12531263
@Test
12541264
void deregistersShutdownHookForFailedApplicationContext() {
12551265
SpringApplication application = new SpringApplication(BrokenPostConstructConfig.class);
@@ -1666,6 +1676,12 @@ static class NotLazyBean {
16661676

16671677
}
16681678

1679+
@Configuration(proxyBeanMethods = false)
1680+
@org.springframework.context.annotation.PropertySource("classpath:spring-application-config-property-source.properties")
1681+
static class PropertySourceConfig {
1682+
1683+
}
1684+
16691685
static class ExitStatusException extends RuntimeException implements ExitCodeGenerator {
16701686

16711687
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test.name=spring-application-config-property-source

0 commit comments

Comments
 (0)