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()));
	}
相關文章
相關標籤/搜索