Created
November 25, 2015 08:57
-
-
Save SlyDen/d7ac9486a088707e04ab to your computer and use it in GitHub Desktop.
Marcel Overdijk's example to override application.properties defaults ... should work in boot after 1.3.1 (bug in 1.3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Order(Ordered.LOWEST_PRECEDENCE) | |
| public class MyFrameworkPropertyDefaultsPostProcessor implements EnvironmentPostProcessor { | |
| private static final String DEFAULT_MY_FRAMEWORK_PROPERTIES = "defaultMyFrameworkProperties"; | |
| private static final String DEFAULT_MY_FRAMEWORK_PROPERTIES_LOCATION = "/config/myFramework-application.yml"; | |
| private YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader(); | |
| @Override | |
| public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { | |
| Resource myFrameworkDefaults = new ClassPathResource(DEFAULT_MY_FRAMEWORK_PROPERTIES_LOCATION); | |
| String[] profiles = environment.getActiveProfiles(); | |
| load(environment, myFrameworkDefaults, null); | |
| if (profiles.length > 0) { | |
| for (String profile : profiles) { | |
| load(environment, myFrameworkDefaults, profile); | |
| } | |
| } | |
| } | |
| private void load(ConfigurableEnvironment environment, Resource resource, String profile) { | |
| try { | |
| PropertySource<?> propertySource = propertySourceLoader.load(DEFAULT_MY_FRAMEWORK_PROPERTIES, resource, profile); | |
| if (propertySource != null) { | |
| environment.getPropertySources().addLast(propertySource); | |
| } | |
| } catch (IOException e) { | |
| // ignore | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment