溫馨提示×

溫馨提示×

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

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

spring boot中的properties參數(shù)配置詳解

發(fā)布時間:2020-10-01 12:52:29 來源:腳本之家 閱讀:188 作者:給人生一個夢 欄目:編程語言

application.properties

application.properties是spring boot默認的配置文件,spring boot默認會在以下兩個路徑搜索并加載這個文件

src\main\resources
src\main\resources\config

配置系統(tǒng)參數(shù)

在application.properties中可配置一些系統(tǒng)參數(shù),spring boot會自動加載這個參數(shù)到相應的功能,如下

#端口,默認為8080 
server.port=80 
#訪問路徑,默認為/ 
server.context-path=/test 
#輸出日志文件,默認不輸出  
logging.file=/log.txt 
#修改日志級別,默認為INFO 
logging.level.root=DEBUG 

自定義properties文件

在spring boot啟動類或配置類中添加以下注解,可在啟動時載入自定義的配置文件

@PropertySource("classpath:config/xxx.properties") 

如果要同時載入多個文件

@PropertySource(value={"classpath:config/a.properties","classpath:config/b.properties"}) 

自定義參數(shù)

以自命名配置一些參數(shù),如

key1=values1 
key2=values2 

在JAVA代碼中,使用@Value注解,在項目啟動時會將自定義參數(shù)加載到全局變量,如下

@RestController 
public class SampleController { 
  @Value(value="${key1}") 
  private String key; 

批量注入到類變量

在properties中配置兩個以a為前綴的參數(shù)

a.key1=values1 
a.key2=values2 

在JAVA中用@ConfigurationProperties 將以a為前綴的參數(shù)注入到當前變量中,需要有setXxx()方法

@RestController 
@ConfigurationProperties(prefix = "a") 
public class SampleController { 
  private String key1; 
  private String key2; 
  public void setKey1(String key1) { 
    this.key1 = key1; 
  } 
  public void setKey2(String key2) { 
    this.key2 = key2; 
  } 

總結

以上所述是小編給大家介紹的spring boot中的properties參數(shù)配置詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

向AI問一下細節(jié)

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

AI