0

Is it possible to control order of resolution from Spring Environment from underlying PropertySources?

For example.

I have OS environment variable

ENV key=os

and run java with System environment variable

java -Dkey=system

What the output of Invoking method m on bean of class A? How to change order of scanning SystemEnvironmentPropertySource or MapPropertySource [name='systemProperties']?

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.inject.Inject;

@Component
class A{
    @Inject
    private Environment environment;

    public void m(){
        System.out.println(environment.getProperty("key"));
    }
}
qwazer
  • 7,174
  • 7
  • 44
  • 69

1 Answers1

0

I found answer, sorry for inconvenience:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/StandardEnvironment.html

```` In addition to the usual functions of a ConfigurableEnvironment such as property resolution and profile-related operations, this implementation configures two default property sources, to be searched in the following order:

system properties
system environment variables 

That is, if the key "xyz" is present both in the JVM system properties as well as in the set of environment variables for the current process, the value of key "xyz" from system properties will return from a call to environment.getProperty("xyz"). This ordering is chosen by default because system properties are per-JVM, while environment variables may be the same across many JVMs on a given system. Giving system properties precedence allows for overriding of environment variables on a per-JVM basis.

These default property sources may be removed, reordered, or replaced; and additional property sources may be added using the MutablePropertySources instance available from AbstractEnvironment.getPropertySources(). See ConfigurableEnvironment Javadoc for usage examples. ````

qwazer
  • 7,174
  • 7
  • 44
  • 69