SpringBoot讀取配置文件的四種方式
引言
在 Spring Boot 中,application.yml
文件用于配置應(yīng)用程序的屬性,Spring Boot 默認(rèn)會從 src/main/resources
目錄下的 application.properties
或 application.yml
文件中讀取配置。讀取 application.yml
文件中的配置可以通過以下幾種常用的方法進(jìn)行:
@Value
: 適合讀取簡單的單一屬性。@ConfigurationProperties
: 適合讀取復(fù)雜的配置集。Environment
: 適合在運行時動態(tài)訪問配置。@PropertySource
: 加載.properties
文件。
1. @Value 注解讀取單個屬性
@Value
注解用于讀取單一簡單的配置屬性,該注解可以直接應(yīng)用于字段、構(gòu)造函數(shù)或方法參數(shù)上,不要求提供 Setter
方法。
在
application.yml
配置文件中添加如下配置:
app: name: MyApp keys: - key1 - key2
使用 @Value
注解從配置文件中讀取單個屬性值:
- 字段注入:將
@Value
注解應(yīng)用于字段,Spring 將直接將配置值注入到字段中。
@Component public class MyComponent { @Value("${app.name}") private String name; }
- 構(gòu)造函數(shù)注入:
@Value
也可以用于構(gòu)造函數(shù)參數(shù)。
@Component public class MyComponent { private final String name; public MyComponent(@Value("${app.name}") String name) { this.name = name; } }
- 方法參數(shù)注入:
@Value
注解用于方法參數(shù)。
@Component public class MyComponent { private String name; @Value("${app.name}") public void setKeys(String name) { this.name = name; } }
注意:@Value
注解無法處理復(fù)雜的配置,如集合,如下處理會在應(yīng)用啟動時拋出 IllegalArgumentException
異常:Could not resolve placeholder 'app.keys' in value "${app.keys}"
@Value("${app.keys}") private List<String> keys;
2. 使用 @ConfigurationProperties 注解
@ConfigurationProperties
用于將配置文件中的屬性綁定到 Java 對象中。適合處理結(jié)構(gòu)化的配置,比如嵌套的屬性或復(fù)雜的屬性集合。
- 在
application.yml
配置文件中添加如下配置:
app: name: MyApp keys: - key1 - key2
在類上使用 @ConfigurationProperties
注解,并指定前綴,該類會直接映射配置文件中的屬性名一致屬性:
@Component @ConfigurationProperties(prefix = "app") public class AppProperties { private List<String> keys; }
注意:@ConfigurationProperties 讀取配置時需要確保配置文件中的屬性名與 Java 類中的屬性名匹配。
3. 通過 Environment 對象讀取屬性
Environment 是 Spring 中用于管理和訪問配置屬性、配置文件和環(huán)境變量的一個抽象接口,該對象提供了一種管理和訪問配置屬性、激活的配置文件和環(huán)境變量的統(tǒng)一方式。我們可以在任何 Spring 管理的 bean 中通過注入 Environment 對象來獲取配置屬性。
- 在 application.yml 配置文件中添加如下配置:
app: name: MyApp keys: - key1 - key2
- 在類中注入
Environment
對象,使用env.getProperty(String key)
來檢索屬性的值。
@Component public class MyComponent { @Autowired private Environment env; public void printConfig() { String port = env.getProperty("server.port"); System.out.println("Server Port: " + port); } }
注意:Environment
在加載 YML 配置文件時,會將每個元素視為單獨的鍵。
即 application.yml
文件中的配置:
yamlCopy Codeapp: keys: - key1 - key2 - key3
在 Environment
中,將被處理為:
app.keys[0]
對應(yīng)key1
app.keys[1]
對應(yīng)key2
app.keys[2]
對應(yīng)key3
因此對于復(fù)雜配置需要特殊處理,例如獲取集合元素需要使用類似數(shù)組索引的語法來訪問 YAML 文件中的列表元素。
4. 使用 @PropertySource 注解加載額外的配置文件
@PropertySource 注解是 Spring Framework 提供的一個注解,用于加載額外 .properties 配置文件或其他資源文件中的屬性到 Spring 的 Environment 中。
在 src/main/resources
目錄下創(chuàng)建 config.properties
的屬性文件:
app.name=MyApp app.version=1.0.0
在配置類上使用 @PropertySource
注解來加載配置文件:
@Configuration @PropertySource("classpath:config.properties") //@PropertySource({"classpath:config.properties", "classpath:another-config.properties"}) public class AppConfig { }
使用 @Value
注解或 Environment
對象獲取屬性:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyComponent { @Value("${app.name}") private String appName; @Autowired private Environment env; public void printAppInfo() { System.out.println("App Name: " + appName); String appVersion = env.getProperty("app.version"); System.out.println("App Version: " + appVersion); } }
注意: 如果需要加載多個屬性文件,可以在 @PropertySource
注解中使用 value
屬性指定多個文件路徑:
@PropertySource({"classpath:config.properties", "classpath:another-config.properties"})
- 路徑:classpath: 前綴表示文件在類路徑中。如果文件位于文件系統(tǒng)的其他位置,你可以使用文件系統(tǒng)路徑,例如 file:/path/to/config.properties。
- 覆蓋:@PropertySource 加載的屬性文件中的屬性會覆蓋同名的系統(tǒng)屬性,但不會覆蓋由 Spring Boot 的 application.properties 或 application.yml 文件中定義的屬性。
- 順序:如果多個屬性文件定義了同一個屬性,后加載的文件中的屬性值會覆蓋先加載的文件中的值。
以上就是SpringBoot讀取配置文件的四種方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot讀取配置文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Springboot項目啟動后自動創(chuàng)建多表關(guān)聯(lián)的數(shù)據(jù)庫與表的方案
這篇文章主要介紹了解決Springboot項目啟動后自動創(chuàng)建多表關(guān)聯(lián)的數(shù)據(jù)庫與表的方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03關(guān)于SpringBoot配置項的優(yōu)先級,不再有配置不生效的問題
這篇文章主要介紹了關(guān)于SpringBoot配置項的優(yōu)先級,不再有配置不生效的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04詳解SpringBoot Mybatis如何對接多數(shù)據(jù)源
這篇文章主要為大家介紹了SpringBoot Mybatis如何對接多數(shù)據(jù)源實現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09Java快速實現(xiàn)PDF轉(zhuǎn)圖片功能實例代碼
PDFBox是一個開源Java類庫,用于讀取和創(chuàng)建PDF文檔,它支持文本提取、表單處理、文檔加密解密、合并分割、內(nèi)容覆蓋追加、文檔打印和轉(zhuǎn)換等功能,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09