PeeWee2201
PeeWee2201

Reputation: 1524

Exclude Java System properties and environment variables from SpringBoot configuration

Regarding the property resolution of SpringBoot explained here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

I want to exclude from the mechanism:

9.  Java System properties (System.getProperties()).
10. OS environment variables.

Is it possible?

Thanks

Upvotes: 3

Views: 2522

Answers (1)

glytching
glytching

Reputation: 47865

You can provide your own implementation of StandardEnvironment when instancing your Spring Boot application.

For example:

public static void main(String[] args) {
    SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder(Application.class)
            .environment(new StandardEnvironment(){
                @Override
                protected void customizePropertySources(MutablePropertySources propertySources) {
                    // do not add system or env properties to the set of property sources
                }
            });
    applicationBuilder.run(args);
}

Or alternatively:

public static void main(String[] args) {
    SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder(Application.class)
            .environment(new StandardEnvironment(){
                @Override
                public Map<String, Object> getSystemEnvironment() {
                    return new HashMap<>();
                }

                @Override
                public Map<String, Object> getSystemProperties() {
                    return new HashMap<>();
                }
            });
    applicationBuilder.run(args);
}

Either way, you ensure that your application's properties do not contain any system or environment properties.

Upvotes: 4

Related Questions