SpringBoot3讀取配置文件application.properties屬性值的幾種方式
在 Spring Boot
項(xiàng)目中,可以通過(guò)以下幾種方式讀取 application.properties
文件中的配置屬性值:
1. 使用 @Value 注解讀取單個(gè)屬性值
@Value
注解可以直接用于注入 application.properties
文件中的屬性值。
示例
假設(shè)在 application.properties
中定義了以下配置項(xiàng):
app.name=MyApp app.version=1.0.0
可以在任意 @Component
、@Service
、@Controller
等注解標(biāo)注的類(lèi)中,通過(guò) @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
注解允許批量讀取具有相同前綴的屬性,并將其注入到一個(gè) Java 類(lèi)中。這種方式適用于管理大量的配置項(xiàng)。
示例
假設(shè)在 application.properties
中定義了一組 app
前綴的屬性:
app.name=MyApp app.version=1.0.0 app.description=A sample application
可以創(chuàng)建一個(gè)配置類(lèi),并使用 @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; } }
在其他類(lèi)中可以通過(guò)注入 AppConfigProperties
類(lèi)來(lái)獲取配置值:
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
注解類(lèi)必須是一個(gè)@Component
,并且需要在application.properties
中啟用屬性綁定支持,通常在 Spring Boot 中默認(rèn)已啟用。
3. 使用 Environment 讀取屬性
Environment
接口提供了動(dòng)態(tài)獲取屬性值的方式,適合用于在運(yùn)行時(shí)讀取配置文件中的屬性。
示例
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
接口適合在需要?jiǎng)討B(tài)獲取配置或配置項(xiàng)不確定的場(chǎng)景中使用。
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 文件屬性值的幾種常見(jiàn)方式:
- @Value 注解:直接讀取單個(gè)屬性值。
- @ConfigurationProperties 注解:批量讀取具有相同前綴的屬性。
- Environment 接口:動(dòng)態(tài)讀取屬性,適合運(yùn)行時(shí)獲取。
- @PropertySource 注解:加載自定義配置文件的屬性值。
以上就是SpringBoot3讀取配置文件application.properties屬性值的幾種方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot3讀取application.properties屬性值的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
一文詳解SpringBoot使用Kafka如何保證消息不丟失
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Kafka如何保證消息不丟失的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2025-01-01關(guān)于SpringBoot 打包成的可執(zhí)行jar不能被其他項(xiàng)目依賴(lài)的問(wèn)題
這篇文章主要介紹了關(guān)于SpringBoot 打包成的可執(zhí)行jar不能被其他項(xiàng)目依賴(lài)的問(wèn)題,本文給大家通過(guò)圖文實(shí)例相結(jié)合給大家分享解決方法,需要的朋友可以參考下2020-10-10idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案
JSON(JavaScript Object Notation,JS對(duì)象簡(jiǎn)譜)是一種輕量級(jí)的數(shù)據(jù)交換格式,這篇文章主要介紹了idea pom導(dǎo)入net.sf.json的jar包失敗的解決方案,感興趣的朋友一起看看吧2023-11-11如何解決IDEA沒(méi)有新建servlet選項(xiàng)問(wèn)題
這篇文章主要介紹了如何解決IDEA沒(méi)有新建servlet選項(xiàng)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04java編程調(diào)用存儲(chǔ)過(guò)程中得到新增記錄id號(hào)的實(shí)現(xiàn)方法
這篇文章主要介紹了java編程調(diào)用存儲(chǔ)過(guò)程中得到新增記錄id號(hào)的實(shí)現(xiàn)方法,涉及Java數(shù)據(jù)庫(kù)操作中存儲(chǔ)過(guò)程的相關(guān)使用技巧,需要的朋友可以參考下2015-10-10Java的外部類(lèi)為什么不能使用private和protected進(jìn)行修飾的講解
今天小編就為大家分享一篇關(guān)于Java的外部類(lèi)為什么不能使用private和protected進(jìn)行修飾的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04java報(bào)錯(cuò)Cause: java.sql.SQLException問(wèn)題解決
本文主要介紹了java報(bào)錯(cuò)Cause: java.sql.SQLException問(wèn)題解決,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08