Skip to content

Load custom starter properties files based on profile with spring boot v3.0 #37377

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
rahul169sharma opened this issue Sep 14, 2023 · 2 comments
Labels
for: stackoverflow A question that's better suited to stackoverflow.com status: invalid An issue that we don't feel is valid

Comments

@rahul169sharma
Copy link

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.

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

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.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Sep 14, 2023
@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid for: stackoverflow A question that's better suited to stackoverflow.com and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 14, 2023
@wilkinsona
Copy link
Member

On second thoughts, if changes are needed, I think this would be a duplicate of #24688.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: stackoverflow A question that's better suited to stackoverflow.com status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants