SpringBoot中讀取配置文件的6種方式總結(jié)
概述:本文從讀取默認配置文件即自定義配置文件入手,去整理了解幾種加載方案的區(qū)別
SpringBoot讀取配置文件的幾種方式
- 測試方式1:通過Environment讀取配置信息
- 測試方式2:通過@Value注解讀取配置信息(推薦使用)
- 測試方式3:通過@ConfigurationProperties注解讀取配置信息
- 測試方式4:通過@PropertySource+@Value注解讀取配置信息
- 測試方式5:通過@PropertySource+@ConfigurationProperties注解讀取配置信息
- 測試方式6:通過Properties讀取配置信息
結(jié)論:無論什么場景都推薦使用@Value注解準備錯;其他了解即可。
準備工作
配置文件目錄

application.properties
server.port=8080 spring.profiles.active=dev
application-dev.properties
spring.redis.host=localhost logging.level.root = info
application-prod.properties
spring.redis.port=6379 logging.level.root = warn
my.properties
demo.name=cat
案例說明
測試方式1:通過Environment讀取配置信息
注意點說明:
注意點1:Environment是用來讀取應用程序運行時的環(huán)境變量的類,可以通過key-value的方式讀取application.properties和系統(tǒng)環(huán)境變量,命令行輸入?yún)?shù),系統(tǒng)屬性等.
Controller
import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 測試讀取配置文件的幾種方式:
* @Author 211145187
* @Date 2022/7/20 14:02
**/
@RestController
public class ReadApplicationProperties {
@Autowired
private Environment environment;
//測試方式1:通過Environment讀取配置信息
@GetMapping("/readApplicationProperties1")
public Map<String,Object> readApplicationProperties1(){
Map<String,Object> map = new HashMap<>();
map.put("port",environment.getProperty("server.port"));
System.out.println("通過Environment讀取配置信息:" + environment.getProperty("server.port"));
return map;
}
}
結(jié)果打?。?/p>
![]()
測試方式2:通過@Value注解讀取配置信息(推薦使用)
Controller
import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 測試讀取配置文件的幾種方式:
* @Author 211145187
* @Date 2022/7/20 14:02
**/
@RestController
public class ReadApplicationProperties {
@Value("${server.port}")
private Integer serverPort;
//測試方式2:通過@Value注解讀取配置信息
@GetMapping("/readApplicationProperties2")
public void readApplicationProperties2(){
System.out.println("通過@Value注解讀取配置信息:" + serverPort);
}
}
結(jié)果打印
![]()
測試方式3:通過@ConfigurationProperties注解讀取配置信息
注意點說明:
注意點1:@ConfigurationProperties注解用于指定前綴,下方的屬性名稱必須和要獲取的配置信息名稱一致,比如必須叫port,否則獲取值為null
使用@ConfigurationProperties首先建立配置文件與對象的映射關(guān)系,然后在控制器方法中使用@Autowired注解將對象注入.
注意點2:配置生效的兩種方式:
- 方式1:配置@Component
- 方式2:啟動類添加@EnableConfigurationProperties(ReadProperties.class)
總結(jié):注解@Component和注解@EnableConfigurationProperties(ReadProperties.class)是等價的,寫一個就行。
注意點3:@ConfigurationProperties也可以和@Value和@Bean一起使用,只不過我沒寫案例。
注意點4:@ConfigurationProperties只能加載以application為前綴開頭的配置文件,比如application-dev.properties,加載自定義名稱配置文件內(nèi)容無效。
注意點5:
問題:從上面的示例中,我們可以看到在屬性綁定中@EnableConfigurationProperties和@Component的效果一樣,那么為啥springboot還要使用這個注解呢?
答案:當我們引用第三方jar包時,@Component標注的類是無法注入到spring容器中的,這時我們可以用@EnableConfigurationProperties來代替@Component
ReadProperties
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 1)注解@ConfigurationProperties中的prefix用于設置前綴
* 2)下方的屬性名稱必須和要獲取的配置信息名稱一致,比如必須叫port,否則獲取值為null
*/
@ConfigurationProperties(prefix = "server")//這個注解是用找到類
@Component //生效的兩種方式:方式1:配置@Component,方式2:啟動類添加@EnableConfigurationProperties(ReadProperties.class)
@Data
public class ReadProperties {
private Integer port;
}
Controller
import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 測試讀取配置文件的幾種方式:
* @Author 211145187
* @Date 2022/7/20 14:02
**/
@RestController
public class ReadApplicationProperties {
@Autowired
private ReadProperties readProperties;
//測試方式3:通過@ConfigurationProperties注解讀取配置信息
@GetMapping("/readApplicationProperties3")
public void readApplicationProperties3(){
System.out.println("通過@ConfigurationProperties注解讀取配置信息:" + readProperties.getPort());
}
}
結(jié)果打印
![]()
測試方式4:通過@PropertySource+@Value注解讀取配置信息
注意點說明:
注意點1:@PropertySource注解加載指定的屬性文件(*.properties)到 Spring 的 Environment 中??梢耘浜?@Value 和@ConfigurationProperties 和@Bean使用。
注意點2:@PropertySource注解可以配合 @Value 和@ConfigurationProperties 和@Bean一起使用,只不過我沒寫案例。
注意點3:使用@PropertySource注解推薦只加載自定義名稱的配置文件,不要加載以application為前綴開頭的配置文件,比如application-dev.properties,因為重名的key值會被覆蓋,這點會在注意點4中著重說明。
注意點4:(最容易出錯)
講解一個大坑
補充說明:application-dev.properties中設置logging.level.root = info,而application-prod.properties中設置logging.level.root = warn
案例說明:application.properties配置文件設置內(nèi)置spring.profiles.active=dev,用于關(guān)聯(lián)application-dev.properties配置文件,正常代碼運行會把application.properties和application-dev.properties配置文件都加載到內(nèi)存中,但是現(xiàn)在我想創(chuàng)建一個config或者bean,通過@PropertySource注解去注入并打印application-prod.properties中的這個內(nèi)容:logging.level.root = warn,正確打印logging.level.root的結(jié)果應該是warn,因為它是最后加載的,但實際打印結(jié)果logging.level.root的值是info,
問題:為什么?為什么打印info,而我想打印的是prod中的值warn
答案:如圖1,你看紅色框中你感覺prod在info下載加載,你會覺得prod相同的key會覆蓋dev中的值,實際答案真不是這樣,詳情請看如圖2這個人的回答。正常來說生產(chǎn)項目中application-dev.properties和application-prod.properties只會允許使用一個,才不會混用。
實際真實項目解決方案是:
- 第1種方案:啟動類中配置環(huán)境變量也會通過指定dev還是prod生效,所以該案例只是自己的一個想法,實際沒太大作用且實際項目也不會允許同時加載dev和prod配置文件。
- 第2種方案:為了避免重名key被覆蓋,我們會讓application.properties和application-dev.properties不會存放相同的key內(nèi)容,即application.properties有一個key,那么application-dev.properties和application-prod.properties中絕不會有這個相同的key內(nèi)容。
- 第3種方案:如果非要使用@PropertySource注解注入一個配置文件,那么一定指向自定義名稱配置文件,千萬不要指向以application-為前綴的配置文件。

如圖1

如圖2
注意點5:配置文件加載的優(yōu)先級 > @PropertySource注解注入
ReadProperties2
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @Author 211145187
* @Date 2022/7/20 15:47
**/
@PropertySource(value = {"application.properties"})
@Component
@Data
public class ReadProperties2 {
@Value("${server.port}")
private Integer port;
}
ReadProperties4
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @Author 211145187
* @Date 2022/7/20 15:51
**/
@ConfigurationProperties(prefix = "spring.redis")//這個注解是用找到類 注意:@ConfigurationProperties無法加載自定義配置問價內(nèi)容,必須和@PropertySource配合使用才能獲取
@Component //生效的兩種方式:方式1:配置@Component,方式2:啟動類添加@EnableConfigurationProperties(ReadProperties.class)
@PropertySource(value = {"classpath:application-prod.properties"})
@Data
public class ReadProperties4 {
private String port;
}
Controller
import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 測試讀取配置文件的幾種方式:
* @Author 211145187
* @Date 2022/7/20 14:02
**/
@RestController
public class ReadApplicationProperties {
@Autowired
private ReadProperties2 readProperties2;
//測試方式4:通過@PropertySource+@Value注解讀取配置信息
@GetMapping("/readApplicationProperties4")
public void readApplicationProperties4(){
System.out.println("通過@PropertySource注解讀取配置信息:" + readProperties2.getPort());
}
}
結(jié)果打印
![]()
測試方式5:通過@PropertySource+@ConfigurationProperties注解讀取配置信息
ReadProperties3
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @Author 211145187
* @Date 2022/7/20 15:51
**/
@ConfigurationProperties(prefix = "demo")//這個注解是用找到類 注意:@ConfigurationProperties無法加載自定義配置問價內(nèi)容,必須和@PropertySource配合使用才能獲取
@Component //生效的兩種方式:方式1:配置@Component,方式2:啟動類添加@EnableConfigurationProperties(ReadProperties.class)
@PropertySource(value = {"classpath:my.properties"})
@Data
public class ReadProperties3 {
private String name;
}
Controller
import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 測試讀取配置文件的幾種方式:
* @Author 211145187
* @Date 2022/7/20 14:02
**/
@RestController
public class ReadApplicationProperties {
@Autowired
private ReadProperties3 readProperties3;
//測試方式5:通過@PropertySource+@ConfigurationProperties注解讀取配置信息
@GetMapping("/readApplicationProperties5")
public void readApplicationProperties5(){
System.out.println("通過@PropertySource+@ConfigurationProperties注解讀取配置信息:" + readProperties3);
}
}
結(jié)果打印
![]()
測試方式6:通過Properties讀取配置信息
Controller
import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 測試讀取配置文件的幾種方式:
* @Author 211145187
* @Date 2022/7/20 14:02
**/
@RestController
public class ReadApplicationProperties {
//測試方式6:通過Properties讀取配置信息
@GetMapping("/readApplicationProperties6")
public void readApplicationProperties6() throws IOException {
Resource resource = new ClassPathResource("application-prod.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
String root = properties.getProperty("logging.level.root");
System.out.println("通過xProperties讀取配置信息:" + root);
}
}
結(jié)果打印
![]()
以上就是SpringBoot中讀取配置文件的6種方式總結(jié)的詳細內(nèi)容,更多關(guān)于SpringBoot讀取配置文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot項目War包部署無法注冊到Nacos中的解決
這篇文章主要介紹了SpringBoot項目War包部署無法注冊到Nacos中的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Mac系統(tǒng)上安裝?JDK?8?最穩(wěn)最全教程(Homebrew?方式)
Homebrew是一個開源的包管理器,專為macOS(盡管也可以在Linux上使用)設計,用來簡化在Mac操作系統(tǒng)上安裝軟件的過程,這篇文章主要介紹了Mac系統(tǒng)上使用Homebrew方式安裝JDK8最穩(wěn)最全教程的相關(guān)資料,需要的朋友可以參考下2026-04-04
IntelliJ?IDEA中如何使用Checkstyle對Java代碼規(guī)范檢查
這篇文章主要介紹了IntelliJ IDEA中如何使用Checkstyle對Java代碼規(guī)范檢查的相關(guān)資料,checkstyle是提高代碼質(zhì)量,檢查代碼規(guī)范的很好用的一款工具,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2026-03-03

