Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active August 29, 2023 03:03
Show Gist options
  • Save sandipchitale/09daa7435d29fa9f26f5b3cd8c00fd5e to your computer and use it in GitHub Desktop.
Save sandipchitale/09daa7435d29fa9f26f5b3cd8c00fd5e to your computer and use it in GitHub Desktop.
Set profile based on other profile #springboot #profile
static class PreConfigDataEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
PropertiesPropertySourceLoader propertiesPropertySourceLoader = new PropertiesPropertySourceLoader();
try {
List<PropertySource<?>> propertySources = propertiesPropertySourceLoader.load("Config resource 'class path resource [application.properties]' via location 'optional:classpath:/'",
new ClassPathResource("/application.properties"));
propertySources.stream().forEach((PropertySource<?> propertySource) -> {
Object springProfilesActive = propertySource.getProperty("spring.profiles.active");
if (springProfilesActive instanceof String springProfilesActiveString) {
StringUtils.commaDelimitedListToSet(springProfilesActiveString).stream().forEach((String profile) -> {
if ("india".equals(profile)) {
environment.addActiveProfile("hindi");
}
if ("spain".equals(profile) || "mexico".equals(profile)) {
environment.addActiveProfile("spanish");
}
});
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
}
org.springframework.boot.env.EnvironmentPostProcessor=sandipchitale.appevents.AppeventsApplication.PreConfigDataEnvironmentPostProcessor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment