SpringBoot3讀取配置文件application.properties屬性值的幾種方式
在 Spring Boot
項目中,可以通過以下幾種方式讀取 application.properties
文件中的配置屬性值:
1. 使用 @Value 注解讀取單個屬性值
@Value
注解可以直接用于注入 application.properties
文件中的屬性值。
示例
假設(shè)在 application.properties
中定義了以下配置項:
app.name=MyApp app.version=1.0.0
可以在任意 @Component
、@Service
、@Controller
等注解標(biāo)注的類中,通過 @Value
注解獲取這些值:
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class AppConfig { @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; public void printConfig() { System.out.println("App Name: " + appName); System.out.println("App Version: " + appVersion); } }
注意:
${app.name}
表示從配置文件中讀取app.name
屬性值。
2. 使用 @ConfigurationProperties 注解讀取配置前綴
@ConfigurationProperties
注解允許批量讀取具有相同前綴的屬性,并將其注入到一個 Java 類中。這種方式適用于管理大量的配置項。
示例
假設(shè)在 application.properties
中定義了一組 app
前綴的屬性:
app.name=MyApp app.version=1.0.0 app.description=A sample application
可以創(chuàng)建一個配置類,并使用 @ConfigurationProperties
注解將 app
前綴的屬性注入其中:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "app") public class AppConfigProperties { private String name; private String version; private String description; // Getter and Setter methods public String getName() { return name; } public void setName(String name) { this.name = name; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
在其他類中可以通過注入 AppConfigProperties
類來獲取配置值:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class AppService { private final AppConfigProperties appConfigProperties; @Autowired public AppService(AppConfigProperties appConfigProperties) { this.appConfigProperties = appConfigProperties; } public void printAppInfo() { System.out.println("App Name: " + appConfigProperties.getName()); System.out.println("App Version: " + appConfigProperties.getVersion()); System.out.println("App Description: " + appConfigProperties.getDescription()); } }
注意:
@ConfigurationProperties
注解類必須是一個@Component
,并且需要在application.properties
中啟用屬性綁定支持,通常在 Spring Boot 中默認(rèn)已啟用。
3. 使用 Environment 讀取屬性
Environment
接口提供了動態(tài)獲取屬性值的方式,適合用于在運行時讀取配置文件中的屬性。
示例
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class EnvironmentConfig { private final Environment environment; @Autowired public EnvironmentConfig(Environment environment) { this.environment = environment; } public void printEnvConfig() { String appName = environment.getProperty("app.name"); String appVersion = environment.getProperty("app.version"); System.out.println("App Name: " + appName); System.out.println("App Version: " + appVersion); } }
注意:
Environment
接口適合在需要動態(tài)獲取配置或配置項不確定的場景中使用。
4. 使用 @PropertySource 加載自定義配置文件
除了 application.properties
,也可以定義自定義配置文件,并使用 @PropertySource
注解加載其中的屬性。
示例
- 創(chuàng)建自定義配置文件
custom-config.properties
,內(nèi)容如下:
custom.property1=value1 custom.property2=value2
- 使用
@PropertySource
注解加載自定義配置文件:
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:custom-config.properties") public class CustomConfig { @Value("${custom.property1}") private String property1; @Value("${custom.property2}") private String property2; public void printCustomConfig() { System.out.println("Custom Property 1: " + property1); System.out.println("Custom Property 2: " + property2); } }
注意:確保
custom-config.properties
文件位于classpath
下(如src/main/resources
目錄)。
總結(jié)
以上是 Spring Boot 中讀取 application.properties 文件屬性值的幾種常見方式:
- @Value 注解:直接讀取單個屬性值。
- @ConfigurationProperties 注解:批量讀取具有相同前綴的屬性。
- Environment 接口:動態(tài)讀取屬性,適合運行時獲取。
- @PropertySource 注解:加載自定義配置文件的屬性值。
以上就是SpringBoot3讀取配置文件application.properties屬性值的幾種方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot3讀取application.properties屬性值的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文詳解SpringBoot使用Kafka如何保證消息不丟失
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Kafka如何保證消息不丟失的相關(guān)知識,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2025-01-01關(guān)于SpringBoot 打包成的可執(zhí)行jar不能被其他項目依賴的問題
這篇文章主要介紹了關(guān)于SpringBoot 打包成的可執(zhí)行jar不能被其他項目依賴的問題,本文給大家通過圖文實例相結(jié)合給大家分享解決方法,需要的朋友可以參考下2020-10-10idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案
JSON(JavaScript Object Notation,JS對象簡譜)是一種輕量級的數(shù)據(jù)交換格式,這篇文章主要介紹了idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案,感興趣的朋友一起看看吧2023-11-11java編程調(diào)用存儲過程中得到新增記錄id號的實現(xiàn)方法
這篇文章主要介紹了java編程調(diào)用存儲過程中得到新增記錄id號的實現(xiàn)方法,涉及Java數(shù)據(jù)庫操作中存儲過程的相關(guān)使用技巧,需要的朋友可以參考下2015-10-10Java的外部類為什么不能使用private和protected進行修飾的講解
今天小編就為大家分享一篇關(guān)于Java的外部類為什么不能使用private和protected進行修飾的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04java報錯Cause: java.sql.SQLException問題解決
本文主要介紹了java報錯Cause: java.sql.SQLException問題解決,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08