Set all spring boot properties inside .yml file into system properties

2.3k Views Asked by At

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);
            }));
1

There are 1 best solutions below

1
On

you might use something like this:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Properties;

@Component
public class EnvTest {

    final StandardEnvironment env;

    @Autowired
    public EnvTest(StandardEnvironment env) {
        this.env = env;
    }

    @PostConstruct
    public void setupProperties() {
        for (PropertySource<?> propertySource : env.getPropertySources()) {
            if (propertySource instanceof MapPropertySource) {
                final String propertySourceName = propertySource.getName();
                if (propertySourceName.startsWith("applicationConfig")) {
                    System.out.println("setting sysprops from " + propertySourceName);
                    final Properties sysProperties = System.getProperties();

                    MapPropertySource mapPropertySource = (MapPropertySource) propertySource;
                    for (String key : mapPropertySource.getPropertyNames()) {
                        Object value = mapPropertySource.getProperty(key);
                        System.out.println(key + " -> " + value);
                        sysProperties.put(key, value);
                    }
                }
            }
        }
    }
}

and of course, please remove the stdout messages when it's working for you