溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring怎么讀取配置文件屬性

發(fā)布時(shí)間:2020-08-01 11:16:26 來源:億速云 閱讀:163 作者:小豬 欄目:編程語言

這篇文章主要講解了Spring怎么讀取配置文件屬性,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

一 前言

本篇內(nèi)容包括spring 運(yùn)行時(shí)讀取配置文件的多種方式和SpEl表達(dá)式入門基礎(chǔ);

二運(yùn)行時(shí)讀取配置文件

spring 運(yùn)行時(shí)讀取配置文件值提供了2種方式

屬性占位符(Property placeholder)。

Spring表達(dá)式語言(SpEL)

2.1 讀取外部配置文件

使用 @PropertySource 注解可以讀取導(dǎo)classpath下配置文件屬性;參數(shù)如下

  • value是個(gè)字符串?dāng)?shù)組;
  • ignoreResourceNotFound;如果設(shè)置為true, 配置文件未找到時(shí)不會(huì)報(bào)錯(cuò);
  • encoding;指定字符集

首先resource 目錄下創(chuàng)建配置文件zszxz.properties ; 內(nèi)容如下

zszxz.name = zszxz
zszxz.point = share

其次讀取配置文件配置類如下

@Configuration
@PropertySource(value = {"classpath:zszxz.properties"},encoding = "UTF-8")
@Component
public class EnvironmentProperty {
  // 注入環(huán)境
  @Autowired
  private Environment environment;


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

最后通過測試類調(diào)用outputProperty()輸出配置文件中屬性的值

@RunWith(SpringJUnit4ClassRunner.class)//創(chuàng)建spring應(yīng)用上下文
@ContextConfiguration(classes= EnvironmentProperty.class)//加載配置類
public class PropertyTest {
  @Autowired
  EnvironmentProperty environmentProperty;

  @Test
  public void test(){
    // zszxz
    environmentProperty.outputProperty();
  }
}

Tip 也可以使用@PropertySources 注解,其value是 @PropertySource類型的數(shù)組;

其中 EnvironmentProperty 獲取主要屬性方法如下

  • String getProperty(String key); 通過key 取值
  • String getProperty(String key, String defaultValue); 獲取值,沒有則使用默認(rèn)值;
  • T getProperty(String key, Class var2); 獲取值,指定返回類型;
  • T getProperty(String key, Class var2, T defaultValue);獲取值,指定返回類型,指定默認(rèn)值;
  • 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)//創(chuàng)建spring應(yīng)用上下文
@ContextConfiguration(classes= EnvironmentProperty.class)//加載配置類
public class PropertyTest {
  @Autowired
  EnvironmentProperty environmentProperty;
  @Test
  public void testPoint(){
    // share
    environmentProperty.outputPoint();
  }
}

2.3 SpEl表達(dá)式

Spring表達(dá)式語言(Spring Expression Language,SpEL)是一種靈活的表達(dá)式語言,能夠以簡潔的方式將值裝配到bean屬性或者構(gòu)造器參數(shù)中,此過程中能夠計(jì)算表達(dá)式獲取計(jì)算值;使用@Valjue注解時(shí),SpEL表達(dá)式要放到“#{......}”之中;

獲取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);
  }

獲取屬性示例

注意點(diǎn):username字段必須是public

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

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

獲取靜態(tài)方法示例

其中T()表示運(yùn)算會(huì)得到一個(gè)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);
  }

支持運(yùn)算符如下

  • 算術(shù)運(yùn)算 + 、 - 、 * 、 / 、 % 、 ^
  • 比較運(yùn)算 < 、 > 、 == 、 <= 、 >= 、 lt 、 gt 、 eq 、 le 、 ge
  • 邏輯運(yùn)算 and 、 or 、 not 、 │
  • 正則表達(dá)式 matche

看完上述內(nèi)容,是不是對Spring怎么讀取配置文件屬性有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI