Skip to content

Default properties configured on SpringApplication have higher precedence than properties configured with @PropertySource #31068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lulumatous opened this issue May 17, 2022 · 1 comment
Assignees
Labels
type: regression A regression from a previous release
Milestone

Comments

@lulumatous
Copy link

lulumatous commented May 17, 2022

The Externalized Configuration section of the reference documentation say that order of default properties (SpringApplication.setDefaultProperties()) is lower with @PropertySource, but my test is inconsistent.

My Spring Boot is 2.6.7.


My Test 1

@SpringBootApplication
@PropertySource(value = "classpath:/api.properties")
public class Demo6Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Demo6Application.class);
        Properties properties = new Properties();
        properties.setProperty("api.name","default");
        application.setDefaultProperties(properties);
        ConfigurableApplicationContext run = application.run(args);
        String property = run.getEnvironment().getProperty("api.name");
        System.out.println(String.format("api.name=%s",property));
    }

}

api.properties
api.name=api

My Result 1

api.name=default


api.properties may be not loaded in My Test 1,so I print PropertySources of Environment in console.


My Test 2

@SpringBootApplication
@PropertySource(value = "classpath:/api.properties")
public class Demo6Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Demo6Application.class);
        Properties properties = new Properties();
        properties.setProperty("api.name","default");
        application.setDefaultProperties(properties);
        ConfigurableApplicationContext run = application.run(args);
        run.getEnvironment().getPropertySources().forEach(System.out::println);
    }

}

My Result 2

ConfigurationPropertySourcesPropertySource {name='configurationProperties'}
PropertiesPropertySource {name='systemProperties'}
OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}
RandomValuePropertySource {name='random'}
CachedRandomPropertySource {name='cachedrandom'}
MapPropertySource {name='springCloudClientHostInfo'}
DefaultPropertiesPropertySource {name='defaultProperties'}
ResourcePropertySource {name='class path resource [api.properties]'}

My Thought

Spring Boot 2.4 before, the order is achieved by ConfigFileApplicationListener$PropertySourceOrderingPostProcessor, but Spring Boot 2.4 after, I cannot find a BeanFactoryPostProcessor to do that.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 17, 2022
@wilkinsona
Copy link
Member

Thanks for the report. This looks like a bug to me. The defaultProperties property source is moved to the end by ConfigDataEnvironment:

However, this happens before Framework's ConfigurationClassParser processes the @PropertySource annotation and calls addLast to add the resulting property source. As a result, the property source created via @PropertySource has lower precedence than defaultProperties.

@wilkinsona wilkinsona added type: regression A regression from a previous release and removed status: waiting-for-triage An issue we've not yet triaged labels May 17, 2022
@wilkinsona wilkinsona added this to the 2.5.x milestone May 17, 2022
@wilkinsona wilkinsona changed the title DefaultProperties(SpringApplication.setDefaultProperties()) lost order with @PropertySource Default properties configured on SpringApplication have higher precedence than properties configured with @PropertySource May 17, 2022
@philwebb philwebb self-assigned this May 18, 2022
@philwebb philwebb modified the milestones: 2.5.x, 2.5.14 May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: regression A regression from a previous release
Projects
None yet
Development

No branches or pull requests

4 participants