I had a same motivation - to extract common configuration for typical service into a separate starter/library. I've decided to move with EnvironmentPostProcessor
My commons-web
library has its own application-web.properties
with, lets say, single property:
spring.main.banner-mode: off
And EnvironmentPostProcessor
to pick it up
public class DefaultPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor {
@SneakyThrows
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environment.getPropertySources()
.addLast(new ResourcePropertySource("application-web.properties"));
}
}
To make it works you need to specify a post processor (on a library side) in resources/META-INF/spring.factories
this way:
org.springframework.boot.env.EnvironmentPostProcessor=\
com.surprise.starter.web.properties.DefaultPropertiesEnvironmentPostProcessor
With such solution it's possible to pick up this config on service side and override it in regular application.properties
I'm not using @PropertySource
cause there is nuance with properties ordering & detection:
...
@PropertySource annotations on your @Configuration classes. Please
note that such property sources are not added to the Environment until
the application context is being refreshed. This is too late to
configure certain properties such as logging.* and spring.main.* which
are read before refresh begins.