spring applicaiton 之 Environment

org.springframework.core.env.Environment

环境,设置默认环境,激活开发,测试,生产等环境 该接口继承 PropertyResolver PropertyResolver的做用:根据Key获取value,解决占位符的问题 ${xxx}java

先说说PropertySourcesPropertyResolver

org.springframework.core.env.ConfigurablePropertyResolver

继承 PropertyResolverspring

设置占位符前缀
设置占位符后缀
占位符中的分隔符,默认:(没法转换,就用:后面的值替代)
获取转换器 `ConversionService`,另开一篇文章专门介绍

org.springframework.core.env.AbstractPropertyResolver

实现 ConfigurablePropertyResolveride

内置一个 `ConversionService`,主要目的是实现该方法:  
protected <T> T convertValueIfNecessary(Object value, @Nullable Class<T> targetType)

依赖 PropertyPlaceholderHelper。该类内置一个根据Key获取Valuer的接口
	@FunctionalInterface
	public interface PlaceholderResolver {

		/**
		 * Resolve the supplied placeholder name to the replacement value.
		 * @param placeholderName the name of the placeholder to resolve
		 * @return the replacement value, or {@code null} if no replacement is to be made
		 */
		@Nullable
		String resolvePlaceholder(String placeholderName);
	}
为何不直接传递 AbstractPropertyResolver,接口隔离。Helper类的主要做用实现占位符替换。支持 ${xxx:yyy} ${aa${xxx}:yyyy}

org.springframework.core.env.PropertySourcesPropertyResolver

内部用 PropertySources 存储。PropertySource存储键值对的容器。 全部根据Key获取Value的最终都是从PropertySources获取。测试

org.springframework.core.env.ConfigurableEnvironment

设置默认环境,其它环境 合并另一个环境 获取MutablePropertySources,系统参数-D,环境变量(操做系统层)操作系统

org.springframework.core.env.AbstractEnvironment

spring.profiles.active 能够逗号分隔进行配置 spring.profiles.default spring.getenv.ignore 不容许获取环境变量code

org.springframework.core.env.StandardEnvironment

内部是环境变量和系统参数blog

@Override
	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()));
	}
相关文章
相关标签/搜索