1. 程式人生 > 程式設計 >Spring讀取配置檔案屬性實現方法

Spring讀取配置檔案屬性實現方法

一 前言

本篇內容包括spring 執行時讀取配置檔案的多種方式和SpEl表示式入門基礎;

二執行時讀取配置檔案

spring 執行時讀取配置檔案值提供了2種方式

屬性佔位符(Property placeholder)。

Spring表示式語言(SpEL)

2.1 讀取外部配置檔案

使用 @PropertySource 註解可以讀取導classpath下配置檔案屬性;引數如下

  • value是個字串陣列;
  • ignoreResourceNotFound;如果設定為true,配置檔案未找到時不會報錯;
  • encoding;指定字符集

首先resource 目錄下建立配置檔案zszxz.properties ; 內容如下

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()輸出配置檔案中屬性的值

@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型別的陣列;

其中 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

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

https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/expressions.html

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。