SpringBoot讀取配置文件的五種方法總結(jié)
Spring Boot 中讀取配置文件有以下 5 種方法:
- 使用 @Value 讀取配置文件。
- 使用 @ConfigurationProperties 讀取配置文件。
- 使用 Environment 讀取配置文件。
- 使用 @PropertySource 讀取配置文件。
- 使用原生方式讀取配置文件。
它們的具體使用方法如下,為了方便測(cè)試,我們?cè)?Spring Boot 配置文件 application.properties 添加以下內(nèi)容:
profile.name=Spring Boot Profile profile.desc=Spring Boot Profile Desc.
1.使用 @Value 讀取配置文件
使用 @Value 可以讀取單個(gè)配置項(xiàng),如下代碼所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("My Profile Name:" + name);
}
}
以上程序的執(zhí)行結(jié)果如下圖所示:

2.使用 @ConfigurationProperties 讀取配置文件
@ConfigurationProperties 和 @Value 的使用略微不同,@Value 是讀取單個(gè)配置項(xiàng)的,而 @ConfigurationProperties 是讀取一組配置項(xiàng)的,我們可以使用 @ConfigurationProperties 加實(shí)體類讀取一組配置項(xiàng),如下代碼所示:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
private String name;
private String desc;
}
其中 prefix 表示讀取一組配置項(xiàng)的根 name,相當(dāng)于 Java 中的類名,最后再把此配置類,注入到某一個(gè)類中就可以使用了,如下代碼所示:
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Profile profile;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Profile Object:" + profile);
}
}
以上程序的執(zhí)行結(jié)果如下圖所示:

3.使用 Environment 讀取配置文件
Environment 是 Spring Core 中的一個(gè)用于讀取配置文件的類,將此類使用 @Autowired 注入到類中就可以使用它的 getProperty 方法來(lái)獲取某個(gè)配置項(xiàng)的值了,如下代碼所示:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
@SpringBootApplication
public class DemoApplication implements InitializingBean {
@Autowired
private Environment environment;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Profile Name:" + environment.getProperty("profile.name"));
}
}
以上程序的執(zhí)行結(jié)果如下圖所示:

4.使用 @PropertySource 讀取配置文件
使用 @PropertySource 注解可以用來(lái)指定讀取某個(gè)配置文件,比如指定讀取 application.properties 配置文件的配置內(nèi)容,具體實(shí)現(xiàn)代碼如下:
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class DemoApplication implements InitializingBean {
@Value("${profile.name}")
private String name;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Name:" + name);
}
}
以上程序的執(zhí)行結(jié)果如下圖所示:

中文亂碼
如果配置文件中出現(xiàn)中文亂碼的情況,可通過(guò)指定編碼格式的方式來(lái)解決中文亂碼的問(wèn)題,具體實(shí)現(xiàn)如下:
@PropertySource(value = "dev.properties", encoding = "utf-8")
注意事項(xiàng)
@PropertySource 注解默認(rèn)是只支持 properties 格式配置文件的讀取的。
5.使用原生方式讀取配置文件
我們還可以使用最原始的方式 Properties 對(duì)象來(lái)讀取配置文件,如下代碼所示:
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
@SpringBootApplication
public class DemoApplication implements InitializingBean {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void afterPropertiesSet() throws Exception {
Properties props = new Properties();
try {
InputStreamReader inputStreamReader = new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream("application.properties"),
StandardCharsets.UTF_8);
props.load(inputStreamReader);
} catch (IOException e1) {
System.out.println(e1);
}
System.out.println("Properties Name:" + props.getProperty("profile.name"));
}
}
以上程序的執(zhí)行結(jié)果如下圖所示:

總結(jié)
在 Spring Boot 中讀取配置文件有以下 5 種方法:
- 使用 @Value 讀取配置文件。
- 使用 @ConfigurationProperties 讀取配置文件。
- 使用 @PropertySource 讀取配置文件。
- 使用 Environment 讀取配置文件。
- 使用原生方式讀取配置文件。
其中最常用的是前 3 種,如果讀取某一個(gè)配置項(xiàng)可使用 @Value,如果讀取一組配置項(xiàng)可使用 @ConfigurationProperties,如果要指定讀取某一個(gè)具體的配置文件可使用 @PropertySource 來(lái)指定。
到此這篇關(guān)于SpringBoot讀取配置文件的五種方法總結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot讀取配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Spring Security的Oauth2授權(quán)實(shí)現(xiàn)方法
這篇文章主要介紹了基于Spring Security的Oauth2授權(quán)實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Java后臺(tái)接口開(kāi)發(fā)初步實(shí)戰(zhàn)教程
下面小編就為大家分享一篇 Java后臺(tái)接口開(kāi)發(fā)初步實(shí)戰(zhàn)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Java多線程實(shí)現(xiàn)聊天客戶端和服務(wù)器
這篇文章主要為大家詳細(xì)介紹了Java多線程聊天客戶端和服務(wù)器實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Java+mysql本地圖片上傳數(shù)據(jù)庫(kù)及下載示例
本篇文章主要介紹了Java+mysql本地圖片上傳數(shù)據(jù)庫(kù)及下載示例,具有一定的參加價(jià)值,有興趣的可以了解一下。2017-01-01
Spring Cloud EureKa Ribbon 服務(wù)注冊(cè)發(fā)現(xiàn)與調(diào)用
這篇文章主要介紹了Spring Cloud EureKa Ribbon 服務(wù)注冊(cè)發(fā)現(xiàn)與調(diào)用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02

