spring運行時讀取配置文件屬性

一 前言

本篇內容包括spring 運行時讀取配置文件的多種方式和SpEl表達式入門基礎;以爲文章不錯點贊在看謝謝。html

知識追尋者(Inheriting the spirit of open source, Spreading technology knowledge;)java

二運行時讀取配置文件

spring 運行時讀取配置文件值提供了2種方式正則表達式

  1. 屬性佔位符(Property placeholder)。
  2. Spring表達式語言(SpEL)

2.1 讀取外部配置文件

使用 @PropertySource 註解能夠讀取導classpath下配置文件屬性;參數以下spring

  • value是個字符串數組;
  • ignoreResourceNotFound;若是設置爲true, 配置文件未找到時不會報錯;
  • encoding;指定字符集

首先resource 目錄下建立配置文件zszxz.properties ; 內容以下express

zszxz.name = zszxz
zszxz.point = share
複製代碼

其次讀取配置文件配置類以下數組

@Configuration
@PropertySource(value = {"classpath:zszxz.properties"},encoding = "UTF-8")
@Component
public class EnvironmentProperty {

    // 注入環境
    @Autowired
    private Environment environment;


    public void outputProperty(){
        System.out.println(environment.getProperty("zszxz.name"));
    }

}
複製代碼

最後經過測試類調用outputProperty()輸出配置文件中屬性的值markdown

@RunWith(SpringJUnit4ClassRunner.class)//建立spring應用上下文
@ContextConfiguration(classes= EnvironmentProperty.class)//加載配置類
public class PropertyTest {


    @Autowired
    EnvironmentProperty environmentProperty;

    @Test
    public void test(){
        // zszxz
        environmentProperty.outputProperty();
    }
}
複製代碼

Tip 也能夠使用@PropertySources 註解,其value是 @PropertySource類型的數組;dom

其中 EnvironmentProperty 獲取主要屬性方法以下學習

  • String getProperty(String key); 經過key 取值
  • String getProperty(String key, String defaultValue); 獲取值,沒有則使用默認值;
  • T getProperty(String key, Class var2); 獲取值,指定返回類型;
  • T getProperty(String key, Class var2, T defaultValue);獲取值,指定返回類型,指定默認值;
  • String getRequiredProperty(String key) ; key必須爲非空不然拋出IllegalStateException異常

2.2 使用佔位符獲取配置文件

使用註解@Value獲取配置文件屬性值; 其中值使用佔位符("${........}")方式;測試

配置類示例

@Configuration
@PropertySource(value = {"classpath:zszxz.properties"},encoding = "UTF-8")
@Component
public class EnvironmentProperty {

    @Value("${zszxz.point}")
    private String point;

    public void outputPoint(){
        System.out.println(point);
    }

}
複製代碼

測試示例

@RunWith(SpringJUnit4ClassRunner.class)//建立spring應用上下文
@ContextConfiguration(classes= EnvironmentProperty.class)//加載配置類
public class PropertyTest {


    @Autowired
    EnvironmentProperty environmentProperty;

    @Test
    public void testPoint(){
        // share
        environmentProperty.outputPoint();
    }

}
複製代碼

2.3 SpEl表達式

Spring表達式語言(Spring Expression Language,SpEL)是一種靈活的表達式語言,可以以簡潔的方式將值裝配到bean屬性或者構造器參數中,此過程當中可以計算表達式獲取計算值;使用@Valjue註解時,SpEL表達式要放到「#{......}」之中;

獲取bean示例

@Value("#{environmentProperty}")
    private EnvironmentProperty getBean;

    @Test
    public void testBean(){
        // com.zszxz.property.EnvironmentProperty?EnhancerBySpringCGLIB?8e54e11f@1d9b7cce
        System.out.println(getBean);
    }
複製代碼

獲取方法示例

@Value("#{environmentProperty.getStr()}")
    private String getMethod;

    @Test
    public void testMethod(){
        // 知識追尋者
        System.out.println(getMethod);
    }
複製代碼

獲取屬性示例

注意點:username字段必須是public

@Value("#{environmentProperty.username}")
    private String getField;

    @Test
    public void testField(){
        // 知識追尋者
        System.out.println(getField);
    }
複製代碼

獲取靜態方法示例

其中T()表示運算會獲得一個Class對象;

@Value("#{T(java.lang.Math).random()}")
    private double number;

    @Test
    public void testStatic() {
        // 0.9205474938572363
        System.out.println(number);
    }
複製代碼

非空斷定示例

其中? 表示非空斷定

@Value("#{environmentProperty.username?.toString()}")
    private String notNull;

    @Test
    public void testNotNUll() {
        // 知識追尋者
        System.out.println(notNull);
    }
複製代碼

支持運算符以下

  • 算術運算 + 、 - 、 * 、 / 、 % 、 ^
  • 比較運算 < 、 > 、 == 、 <= 、 >= 、 lt 、 gt 、 eq 、 le 、 ge
  • 邏輯運算 and 、 or 、 not 、 │
  • 條件運算 ?: (ternary) 、 ?: (Elvis)
  • 正則表達式 matches

更多內容讀者自行參考官網學習

docs.spring.io/spring/docs…

源碼: 公衆號 知識追尋者 文章彙總處

本文同步分享在 博客「知識追尋者」(JueJin)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索