If any property is defined in application.properties
, it cannot be overridden by re-defining it in other .properties
files using the @PropertySource
annotation. This bug is only valid if the original value is present in application.properties
. Any other file (e.g. app.properties
) will allow to successfully override its values.
E.g.:
application.properties
:
test.application=original
app.properties
:
test.app=original
override.properties
:
test.application=overridden
test.app=overridden
ApplicationPropertiesConfig.java
:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@Data
@PropertySource(value = {
"classpath:application.properties",
"classpath:app.properties",
"classpath:override.properties"
})
@ConfigurationProperties(prefix = "test")
public class ApplicationPropertiesConfig {
private String application; // == "original"
private String app; // == "overridden"
}
In the configuration above, the field application
won't be overridden upon bean creation and will keep its original value defined in application.properties
.
This was confirmed with the most recent Spring Boot versions: 2.7.11 and 3.0.6.
The same behavior is observed even if application.properties
is not listed under @PropertySource
or if the @PropertySources
annotation is used.
! A known workaround is renaming
application.properties
to something else (e.g.app.properties
). Overriding works normally then. But this will not work in my case due to the size of the project where many classes already rely on the defaultapplication.properties
. ! ! Source: a comment by Maksim Muruev (mmuruev) in @PropertySource not overriding in order [SPR-13500].
StackOverflow cross-post: https://stackoverflow.com/questions/76223453/propertysource-doesnt-override-properties-defined-in-application-properties
Code reproducing the issue is here: https://github.com/denisab85/spring-property-overriding
Comment From: quaff
application.properties
is auto registered by spring boot ConfigDataEnvironmentPostProcessor
, and it take precede over @PropertySource
.
If you are using spring-framework and no spring-boot, then application.properties
is overridable.
It's a spring-boot issue, you report the wrong place.
Comment From: snicoll
If any property is defined in application.properties, it cannot be overridden by re-defining it in other .properties files using the @PropertySource annotation
This is the expected behavior. As @quaff mentioned, this is the wrong place to report this, Spring Boot is managed in a dedicated issue tracker.
Comment From: denisab85
This question was very well answered on StackOverflow. Two working solutions were offered in the accepted answer.