Spring PropertyResolver 占位符解析(一)API 介紹
阿新 • • 發佈:2019-01-18
系統屬性 sse .com resources ace illegal 需要 nta 技術 類型轉換又是委托 ConversionService 完成的。
Spring PropertyResolver 占位符解析(一)API 介紹
Spring 系列目錄(https://www.cnblogs.com/binarylei/p/10117436.html)
Spring 3.1 提供了新的占位符解析器 PropertyResolver,默認實現為 PropertySourcesPropertyResolver。相關文章如下:
- Spring PropertyResolver 占位符解析(一)API 介紹
- Spring PropertyResolver 占位符解析(二)源碼分析
一、PropertyResolver API
PropertyResolver 的默認實現是 PropertySourcesPropertyResolver,Environment 實際上也是委托 PropertySourcesPropertyResolver 完成 占位符的解析和類型轉換。
public interface PropertyResolver { // 1. contains boolean containsProperty(String key); // 2.1 獲取指定 key,不存在可以指定默認值,也可以拋出異常 String getProperty(String key); String getProperty(String key, String defaultValue); // 2.2 類型轉換,委托 ConversionService 完成 <T> T getProperty(String key, Class<T> targetType); <T> T getProperty(String key, Class<T> targetType, T defaultValue); String getRequiredProperty(String key) throws IllegalStateException; <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException; // 3. 解析占位符 ${key} String resolvePlaceholders(String text); String resolveRequiredPlaceholders(String text) throws IllegalArgumentException; }
PropertyResolver 組件完成了兩件事:一是占位符解析 ${key};二是類型轉換。使用方法如下:
@Test public void PropertyResolverTest() { PropertySource propertySource = new MapPropertySource("source", Collections.singletonMap("name", "binarylei")); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(propertySource); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); Assert.assertEquals("binarylei", propertyResolver.getProperty("name")); Assert.assertEquals("name is binarylei", propertyResolver.resolvePlaceholders("name is ${name}")); }
二、Spring 是如何使用的
(1) xml 配置
<context:property-placeholder
location="屬性文件,多個之間逗號分隔"
file-encoding="文件編碼"
ignore-resource-not-found="是否忽略找不到的屬性文件"
ignore-unresolvable="是否忽略解析不到的屬性,如果不忽略,找不到將拋出異常"
properties-ref="本地Properties配置"
local-override="是否本地覆蓋模式,即如果true,那麽properties-ref的屬性將覆蓋location加載的屬性,否則相反"
system-properties-mode="系統屬性模式,默認ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永遠不用ENVIRONMENT的,OVERRIDE類似於ENVIRONMENT"
order="順序"
/>
location
:表示屬性文件位置,多個之間通過如逗號/分號等分隔;file-encoding
:文件編碼;ignore-resource-not-found
:如果屬性文件找不到,是否忽略,默認 false,即不忽略,找不到將拋出異常ignore-unresolvable
:是否忽略解析不到的屬性,如果不忽略,找不到將拋出異常properties-ref
:本地 java.util.Properties 配置local-override
:是否本地覆蓋模式,即如果 true,那麽 properties-ref 的屬性將覆蓋 location 加載的屬性system-properties-mode
:系統屬性模式,ENVIRONMENT(默認),NEVER,OVERRIDEENVIRONMENT
:將使用 Spring 3.1 提供的 PropertySourcesPlaceholderConfigurer,其他情況使用 Spring 3.1 之前的 PropertyPlaceholderConfigurer。如果是本地覆蓋模式:那麽查找順序是:properties-ref、location、environment,否則正好反過來;OVERRIDE
: PropertyPlaceholderConfigurer 使用,因為在 spring 3.1 之前版本是沒有 Enviroment 的,所以 OVERRIDE 是 spring 3.1 之前版本的 Environment。如果是本地覆蓋模式:那麽查找順序是:properties-ref、location、System.getProperty(), System.getenv(),否則正好反過來;NEVER
:只查找 properties-ref、location;
order
:當配置多個
(2) 註解配置
@Configuration
@PropertySource(value = "classpath:resources.properties", ignoreResourceNotFound = false)
public class AppConfig {
// 如果想進行 Bean 屬性的占位符替換,需要註冊 PropertySourcesPlaceholderConfigurer
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
如上配置等價於 XML 中的
(3) 占位符替換
使用 Environment 屬性替換,如:
<context:property-placeholder location="classpath:${env}/resources.properties"/>
<context:component-scan base-package="com.sishuok.${package}"/>
<import resource="classpath:${env}/ctx.xml"/>
同樣可以在註解中使用占位符。
@PropertySource(value = "classpath:${env}/resources.properties")
@ComponentScan(basePackages = "com.sishuok.${package}")
@ImportResource(value = {"classpath:${env}/cfg.xml"})
@Value("${env}")
new ClassPathXmlApplicationContext("classpath:${env}/cfg.xml")
參考:
- 《pring3.1新屬性管理API:PropertySource、Environment、Profile》:https://jinnianshilongnian.iteye.com/blog/2000183
每天用心記錄一點點。內容也許不重要,但習慣很重要!
Spring PropertyResolver 占位符解析(一)API 介紹