spring StandardEnvironment 实例及配置读取顺序
所属分类 spring
浏览量 1113
StandardEnvironment
org.springframework.core.env.StandardEnvironment
public class StandardEnvironment extends AbstractEnvironment
public abstract class AbstractEnvironment implements ConfigurableEnvironment
public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver
配置读取顺序,跟添加顺序有关系
MutablePropertySources
addFirst addLast 注意区别
使用 addLast 加载自定义配置
StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[MapPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}, PropertiesPropertySource {name='PropertiesPropertySource1'}, PropertiesPropertySource {name='PropertiesPropertySource2'}]}
propertySources=[MapPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}, PropertiesPropertySource {name='PropertiesPropertySource1'}, PropertiesPropertySource {name='PropertiesPropertySource2'}
MapPropertySource {name='systemProperties'}
SystemEnvironmentPropertySource {name='systemEnvironment'}
PropertiesPropertySource {name='PropertiesPropertySource1'}
PropertiesPropertySource {name='PropertiesPropertySource2'}
按顺序读取 propertySources 中的配置
env.getSystemEnvironment() 环境变量
env.getSystemProperties() jvm系统参数
StandardEnvironment
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}
System.getProperties()
System.getenv()
例子代码
import java.util.Properties;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.StandardEnvironment;
public class EnvTest {
public static void main(String[] args) throws Exception {
StandardEnvironment env = new StandardEnvironment();
System.out.println(env);
System.out.println(env.getDefaultProfiles());
System.out.println(env.getSystemEnvironment());
System.out.println(env.getSystemProperties());
MutablePropertySources mutablePropertySources = env.getPropertySources();
Properties properties1 = new Properties();
properties1.put("name", "cat1");
Properties properties2 = new Properties();
properties2.put("name", "cat2");
PropertiesPropertySource pps1 = new PropertiesPropertySource("PropertiesPropertySource1",properties1);
PropertiesPropertySource pps2 = new PropertiesPropertySource("PropertiesPropertySource2",properties2);
// mutablePropertySources.addFirst(pps1);
// mutablePropertySources.addFirst(pps2);
mutablePropertySources.addLast(pps1);
mutablePropertySources.addLast(pps2);
System.out.println(env);
// addFirst
// StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[PropertiesPropertySource {name='PropertiesPropertySource2'}, PropertiesPropertySource {name='PropertiesPropertySource1'}, MapPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}]}
// addLast 注意顺序
// StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[MapPropertySource {name='systemProperties'}, SystemEnvironmentPropertySource {name='systemEnvironment'}, PropertiesPropertySource {name='PropertiesPropertySource1'}, PropertiesPropertySource {name='PropertiesPropertySource2'}]}
System.out.println(env.getSystemEnvironment().get("name"));
// -Dname=name-in-jvm-args
System.out.println(env.getSystemProperties().get("name"));
System.out.println(env.getProperty("name"));
}
}
完整代码
https://gitee.com/dyyx/springboothello/blob/master/src/test/java/dyyx/EnvTest.java
System.getenv 与 System.getProperty 的区别
上一篇
下一篇
Iterator 与 ListIterator
eclipse安装jetty插件
人生40条建议
aerospike benchmark 性能压测
Objenesis简介
kafka中的 AR OSR ISR HW 和 LEO