With spring boot v2.3.4 I could use ConfigFileApplicationListener to load properties files from my custom starters by writing a environment post processor like
public class StarterEnvironmentPostProcessor implements EnvironmentPostProcessor {
private final ConfigFileApplicationListener environmentPostProcessor = new ConfigFileApplicationListener();
public StarterEnvironmentPostProcessor(String name) {
environmentPostProcessor.setSearchNames(name);
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
environmentPostProcessor.postProcessEnvironment(environment, application);
}
}
And then from each starter I could subclass this environment post processor like
public class MyStarterEnvironmentPostProcessor extends StarterEnvironmentPostProcessor {
public MyStarterEnvironmentPostProcessor() {
super("my-starter");
}
}
This would result in properties being loaded appropriately i.e. if I ran the application that consumes this starter, with profile dev, then properties would be loaded from my-starter.properties, my-starter-dev.properties, application.properties and application-dev.properties, where the first two come from the starter and the last 2 come from the consuming project.
ConfigFileApplicationListener has been removed in spring boot v3.0. The deprecation warning on the class suggests to use ConfigDataEnvironmentPostProcessor instead.
After debugging I found that StandardConfigDataLocationResolver is used to resolve the properties files to be loaded. It constructs config file names like
String[] configNames = binder.bind(CONFIG_NAME_PROPERTY, String[].class).orElse(DEFAULT_CONFIG_NAMES);
meaning there isn't a way for me to inject my-starter config name here. I do not want the project that consumes the custom starters (there are multiple custom starters) to be aware of the config locations. Rather I want the auto configured custom starters to be responsible for loading the appropriate properties.
How can I achieve this in spring boot v3.0?
I have asked the question on the stackoverflow as well. Trying here since I haven't had a response.
Comment From: wilkinsona
The question on Stack Overflow is less than 24hrs old and is the right place to ask something like this. If it turns out that a change in Spring Boot is required, we can re-open this issue to consider it.
Comment From: wilkinsona
On second thoughts, if changes are needed, I think this would be a duplicate of https://github.com/spring-projects/spring-boot/issues/24688.