Skip to content

Commit fbde59d

Browse files
committed
Merge branch '2.6.x' into 2.7.x
Closes gh-31093
2 parents d5d5997 + 6bce0c5 commit fbde59d

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
@@ -34,8 +34,10 @@
3434
import org.apache.commons.logging.LogFactory;
3535

3636
import org.springframework.beans.BeanUtils;
37+
import org.springframework.beans.BeansException;
3738
import org.springframework.beans.CachedIntrospectionResults;
3839
import org.springframework.beans.factory.config.BeanDefinition;
40+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
3941
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4042
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
4143
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
@@ -58,6 +60,7 @@
5860
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5961
import org.springframework.context.annotation.AnnotationConfigUtils;
6062
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
63+
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
6164
import org.springframework.context.support.AbstractApplicationContext;
6265
import org.springframework.context.support.GenericApplicationContext;
6366
import org.springframework.core.GenericTypeResolver;
@@ -390,6 +393,7 @@ private void prepareContext(DefaultBootstrapContext bootstrapContext, Configurab
390393
if (this.lazyInitialization) {
391394
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
392395
}
396+
context.addBeanFactoryPostProcessor(new PropertySourceOrderingBeanFactoryPostProcessor(context));
393397
// Load the sources
394398
Set<Object> sources = getAllSources();
395399
Assert.notEmpty(sources, "Sources must not be empty");
@@ -1368,4 +1372,28 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) {
13681372
return new LinkedHashSet<>(list);
13691373
}
13701374

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+
13711399
}

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
@@ -1271,6 +1271,16 @@ void bindsEnvironmentPrefixToSpringApplication() {
12711271
assertThat(application.getEnvironmentPrefix()).isEqualTo("my");
12721272
}
12731273

1274+
@Test
1275+
void movesConfigClassPropertySourcesToEnd() {
1276+
SpringApplication application = new SpringApplication(PropertySourceConfig.class);
1277+
application.setWebApplicationType(WebApplicationType.NONE);
1278+
application.setDefaultProperties(Collections.singletonMap("test.name", "test"));
1279+
this.context = application.run();
1280+
assertThat(this.context.getEnvironment().getProperty("test.name"))
1281+
.isEqualTo("spring-application-config-property-source");
1282+
}
1283+
12741284
@Test
12751285
void deregistersShutdownHookForFailedApplicationContext() {
12761286
SpringApplication application = new SpringApplication(BrokenPostConstructConfig.class);
@@ -1660,6 +1670,12 @@ static class NotLazyBean {
16601670

16611671
}
16621672

1673+
@Configuration(proxyBeanMethods = false)
1674+
@org.springframework.context.annotation.PropertySource("classpath:spring-application-config-property-source.properties")
1675+
static class PropertySourceConfig {
1676+
1677+
}
1678+
16631679
static class ExitStatusException extends RuntimeException implements ExitCodeGenerator {
16641680

16651681
@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)