I am working on a spring boot project in which a lot of VM arguments are passed for the application to start i.e. cert location, specific profile types(not dev,qa, prod etc).
I am working on moving all the configuration in a default.yml file.
Problem Statement
The properties set in the default.yml are accessible to only environment interface of spring context i.e. org.springframework.core.env.Environment only and the properties are not set into system properties automatically/by default.
I am setting the property in system by a listner ServletContextListener in the method contextInitialized.
But I dont want to explicitly call out all properties by their name using environment .getProperty(key), rather I want that all the properties available in spring context should be looped/without loop be set into system/environment variables.
Expected Solution
I am looking for an approach using which inside the listner method I can set all properties defined in default.yml file into system properties without accessing the properties by their name.
Below is the approach I am currently following to set active profile extracted from spring env/default.yml into system property. I don't want to get active profile or get any property from yml but want all the properties available inside .yml to be set automatically into system.
Optional.ofNullable(springEnv.getActiveProfiles())
.ifPresent(activeProfiles -> Stream.of(activeProfiles).findFirst().ifPresent(activeProfile -> {
String currentProfile = System.getProperty("spring.profiles.active");
currentProfile = StringUtils.isBlank(currentProfile) ? activeProfile : currentProfile;
System.setProperty("spring.profiles.active", currentProfile);
}));
you might use something like this:
and of course, please remove the stdout messages when it's working for you