I had to experiment to convince myself.
Create the most simple spring boot application from Spring Initializr
Then added 3 properties files to resources dir (first one is already there but empty)
# application.properties
foo=foo in application.properties
bar=bar in application.properties
baz=baz in application.properties
# application-foobar.properties
foo=foo in foobar override properties
bar=bar in foobar override properties
# application-barbaz.properties
bar=bar in barbaz override properties
baz=bar in barbaz override properties
Then I added this @Config class to run at startup:
package com.example.profilesexperiment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
class StartupConfig {
@Autowired
private Environment environment;
@Value("${foo}")
private String foo;
@Value("${bar}")
private String bar;
@Value("${baz}")
private String baz;
@Bean
CommandLineRunner startup() {
return args -> {
System.err.println("Active profiles: " + String.join(", ", environment.getActiveProfiles()));
System.err.println("Foo = " + foo);
System.err.println("Bar = " + bar);
System.err.println("Baz = " + baz);
};
}
}
Then I ran it with different combinations of profiles. You can try that yourself, but here are the outputs of some:
just foobar
java -Dspring.profiles.active=foobar -jar target/profiles-experiment-0.0.1-SNAPSHOT.jar
Active profiles: foobar
Foo = foo in foobar override properties
Bar = bar in foobar override properties
Baz = baz in application.properties
foobar then barbaz
java -Dspring.profiles.active=foobar,barbaz -jar ...
Active profiles: foobar, barbaz
Foo = foo in foobar override properties
Bar = bar in barbaz override properties
Baz = bar in barbaz override properties
barbaz then foobar
java -Dspring.profiles.active=barbaz,foobar -jar ...
Active profiles: barbaz, foobar
Foo = foo in foobar override properties
Bar = bar in foobar override properties
Baz = bar in barbaz override properties
Verdict: clearly, last one wins!
Oh, and also: non-overridden properties merge to one big happy property set (which is the reason I came searching here)