Spring PropertyResolver 佔位符解析(一)API 介紹

Spring PropertyResolver 佔位符解析(一)API 介紹

Spring 系列目錄(http://www.javashuo.com/article/p-kqecupyl-bm.html)html

Spring 3.1 提供了新的佔位符解析器 PropertyResolver,默認實現爲 PropertySourcesPropertyResolver。相關文章以下:java

  1. Spring PropertyResolver 佔位符解析(一)API 介紹
  2. Spring PropertyResolver 佔位符解析(二)源碼分析

1、PropertyResolver API

PropertyResolver 的默認實現是 PropertySourcesPropertyResolver,Environment 實際上也是委託 PropertySourcesPropertyResolver 完成 佔位符的解析和類型轉換。 類型轉換又是委託 ConversionService 完成的。spring

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};二是類型轉換。使用方法以下:api

@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}"));
}

2、Spring 是如何使用的

(1) xml 配置ide

<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,OVERRIDE
    1. ENVIRONMENT:將使用 Spring 3.1 提供的 PropertySourcesPlaceholderConfigurer,其餘狀況使用 Spring 3.1 以前的 PropertyPlaceholderConfigurer。若是是本地覆蓋模式:那麼查找順序是:properties-ref、location、environment,不然正好反過來;
    2. OVERRIDE: PropertyPlaceholderConfigurer 使用,由於在 spring 3.1 以前版本是沒有 Enviroment 的,因此 OVERRIDE 是 spring 3.1 以前版本的 Environment。若是是本地覆蓋模式:那麼查找順序是:properties-ref、location、System.getProperty(), System.getenv(),不然正好反過來;
    3. NEVER:只查找 properties-ref、location;
  • order:當配置多個 時的查找順序,關於順序問題請參考:<http://www.iteye.com/topic/1131688 >

(2) 註解配置源碼分析

@Configuration  
@PropertySource(value = "classpath:resources.properties", ignoreResourceNotFound = false)  
public class AppConfig {    
    // 若是想進行 Bean 屬性的佔位符替換,須要註冊 PropertySourcesPlaceholderConfigurer
    @Bean  
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {  
        return new PropertySourcesPlaceholderConfigurer();  
    }   
}

如上配置等價於 XML 中的 配置。ui

(3) 佔位符替換編碼

使用 Environment 屬性替換,如:spa

<context:property-placeholder location="classpath:${env}/resources.properties"/>  
<context:component-scan base-package="com.sishuok.${package}"/> 
<import resource="classpath:${env}/ctx.xml"/>

一樣能夠在註解中使用佔位符。code

@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")

參考:

  1. 《pring3.1新屬性管理API:PropertySource、Environment、Profile》:https://jinnianshilongnian.iteye.com/blog/2000183

天天用心記錄一點點。內容也許不重要,但習慣很重要!

相關文章
相關標籤/搜索