Skip to content

Commit

Permalink
Merge branch '2.6.x' into 2.7.x
Browse files Browse the repository at this point in the history
Closes gh-31093
  • Loading branch information
philwebb committed May 18, 2022
2 parents d5d5997 + 6bce0c5 commit fbde59d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Expand Up @@ -34,8 +34,10 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.CachedIntrospectionResults;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader;
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
Expand All @@ -58,6 +60,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.GenericTypeResolver;
Expand Down Expand Up @@ -390,6 +393,7 @@ private void prepareContext(DefaultBootstrapContext bootstrapContext, Configurab
if (this.lazyInitialization) {
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
}
context.addBeanFactoryPostProcessor(new PropertySourceOrderingBeanFactoryPostProcessor(context));
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
Expand Down Expand Up @@ -1368,4 +1372,28 @@ private static <E> Set<E> asUnmodifiableOrderedSet(Collection<E> elements) {
return new LinkedHashSet<>(list);
}

/**
* {@link BeanFactoryPostProcessor} to re-order our property sources below any
* {@code @PropertySource} items added by the {@link ConfigurationClassPostProcessor}.
*/
private static class PropertySourceOrderingBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {

private final ConfigurableApplicationContext context;

PropertySourceOrderingBeanFactoryPostProcessor(ConfigurableApplicationContext context) {
this.context = context;
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
DefaultPropertiesPropertySource.moveToEnd(this.context.getEnvironment());
}

}

}
Expand Up @@ -1271,6 +1271,16 @@ void bindsEnvironmentPrefixToSpringApplication() {
assertThat(application.getEnvironmentPrefix()).isEqualTo("my");
}

@Test
void movesConfigClassPropertySourcesToEnd() {
SpringApplication application = new SpringApplication(PropertySourceConfig.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setDefaultProperties(Collections.singletonMap("test.name", "test"));
this.context = application.run();
assertThat(this.context.getEnvironment().getProperty("test.name"))
.isEqualTo("spring-application-config-property-source");
}

@Test
void deregistersShutdownHookForFailedApplicationContext() {
SpringApplication application = new SpringApplication(BrokenPostConstructConfig.class);
Expand Down Expand Up @@ -1660,6 +1670,12 @@ static class NotLazyBean {

}

@Configuration(proxyBeanMethods = false)
@org.springframework.context.annotation.PropertySource("classpath:spring-application-config-property-source.properties")
static class PropertySourceConfig {

}

static class ExitStatusException extends RuntimeException implements ExitCodeGenerator {

@Override
Expand Down
@@ -0,0 +1 @@
test.name=spring-application-config-property-source

0 comments on commit fbde59d

Please sign in to comment.