SpringBoot查看項(xiàng)目配置信息的幾種常見(jiàn)方法
以下是查看Spring Boot項(xiàng)目所有配置信息的幾種方法,包括 Actuator端點(diǎn)、日志輸出、代碼級(jí)獲取 等方式,附帶詳細(xì)步驟和示例:

1. 使用Spring Boot Actuator
Actuator是Spring Boot提供的監(jiān)控和管理工具,包含/configprops端點(diǎn)可查看所有配置屬性。
步驟
1.1 添加依賴
在pom.xml中添加Actuator依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.2 配置暴露端點(diǎn)
在application.yml或application.properties中配置暴露configprops端點(diǎn):
management:
endpoints:
web:
exposure:
include: "configprops,health" # 暴露configprops和health端點(diǎn)
1.3 訪問(wèn)配置信息
啟動(dòng)應(yīng)用后,訪問(wèn):
http://localhost:{port}/actuator/configprops
例如:http://localhost:8080/actuator/configprops
輸出示例
{
"configurations": [
{
"name": "spring.http",
"properties": {
"encoding.auto": {
"value": "false",
"origin": "SpringBootAutoConfiguration"
},
"encoding.charset": {
"value": "UTF-8",
"origin": "Spring Boot default"
}
}
},
...
]
}
2. 通過(guò)日志輸出配置信息
在日志中直接打印所有配置屬性。
步驟
2.1 配置日志級(jí)別
在application.yml中啟用配置屬性日志:
logging:
level:
org.springframework.boot.context.properties: DEBUG
2.2 啟動(dòng)應(yīng)用
啟動(dòng)應(yīng)用后,日志中會(huì)輸出所有配置屬性的加載信息,例如:
DEBUG 12345 --- [ main] o.s.b.c.p.PropertySourceBootstrapConfiguration : Located property source: [...]
DEBUG 12345 --- [ main] o.s.b.c.p.PropertySourceBootstrapConfiguration : Adding property source: [...]
2.3 查看完整配置
若需更詳細(xì)的輸出,可在啟動(dòng)時(shí)添加參數(shù):
java -jar your-app.jar --show-config
此參數(shù)會(huì)輸出所有合并后的配置屬性(Spring Boot 2.3+支持)。
3. 通過(guò)代碼獲取配置信息
在代碼中注入Environment或使用@Value獲取配置屬性。
3.1 獲取所有配置
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
@Autowired
private Environment env;
@GetMapping("/all-config")
public Map<String, Object> getAllProperties() {
return env.getPropertySources()
.stream()
.flatMap(ps -> ps.getPropertyNames().stream()
.map(name -> new AbstractMap.SimpleEntry<>(name, ps.getProperty(name))))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
}
訪問(wèn)接口
訪問(wèn):
http://localhost:8080/all-config
4. 使用Spring Boot DevTools的/env端點(diǎn)
DevTools提供了/env端點(diǎn),可查詢特定配置屬性。
步驟
4.1 添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
4.2 訪問(wèn)端點(diǎn)
訪問(wèn):
http://localhost:8080/actuator/env
或查詢特定屬性:
http://localhost:8080/actuator/env/spring.datasource.url
5. 使用@ConfigurationProperties綁定并打印
將配置屬性綁定到Bean并打印。
步驟
5.1 創(chuàng)建配置類
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "your.prefix")
public class YourConfig {
private String property1;
// getters/setters
}
5.2 打印配置
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class ConfigPrinter implements CommandLineRunner {
@Autowired
private YourConfig config;
@Override
public void run(String... args) throws Exception {
System.out.println("Config Property1: " + config.getProperty1());
}
}
6.關(guān)鍵配置對(duì)比表格
| 方法 | 適用場(chǎng)景 | 優(yōu)點(diǎn) | 缺點(diǎn) |
|---|---|---|---|
| Actuator /configprops | 開(kāi)發(fā)/生產(chǎn)環(huán)境監(jiān)控 | 直接通過(guò)HTTP接口查看所有配置 | 需配置安全策略(避免暴露敏感信息) |
| 日志輸出 | 調(diào)試或啟動(dòng)時(shí)快速查看 | 無(wú)侵入性,適合臨時(shí)調(diào)試 | 需手動(dòng)解析日志內(nèi)容 |
| 代碼獲取 | 需要程序內(nèi)處理配置信息 | 靈活控制輸出格式 | 需編寫代碼 |
| DevTools /env | 開(kāi)發(fā)環(huán)境快速查詢 | 支持查詢單個(gè)屬性 | 需依賴DevTools模塊 |
| @ConfigurationProperties | 需要綁定配置到Bean時(shí) | 類型安全,符合Spring規(guī)范 | 需針對(duì)每個(gè)配置前綴編寫B(tài)ean |
7.注意事項(xiàng)
1.安全配置:
生產(chǎn)環(huán)境需限制Actuator端點(diǎn)訪問(wèn),例如:
management:
endpoints:
web:
exposure:
include: "health"
security:
enabled: true
2.敏感信息過(guò)濾:
避免暴露敏感配置(如密碼),可通過(guò)management.endpoints.web.cors.allowed-origins或安全策略控制訪問(wèn)。
3.性能影響:
/configprops端點(diǎn)在配置復(fù)雜時(shí)可能返回大量數(shù)據(jù),需注意性能。
8.完整示例代碼
application.yml
spring:
application:
name: config-demo
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
management:
endpoints:
web:
exposure:
include: "configprops,health"
pom.xml依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
通過(guò)上述方法,可根據(jù)需求選擇最適合的配置查看方式。如需進(jìn)一步優(yōu)化或解決特定問(wèn)題(如安全配置、日志過(guò)濾),可提供具體場(chǎng)景!
到此這篇關(guān)于SpringBoot查看項(xiàng)目配置信息的幾種常見(jiàn)方法的文章就介紹到這了,更多相關(guān)SpringBoot查看項(xiàng)目配置信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus更新對(duì)象時(shí)將字段值更新為null的四種常見(jiàn)方法
MyBatis-Plus 是一個(gè) MyBatis 的增強(qiáng)工具,在簡(jiǎn)化開(kāi)發(fā)、提高效率方面表現(xiàn)非常出色,而,在使用 MyBatis-Plus 更新對(duì)象時(shí),默認(rèn)情況下是不會(huì)將字段值更新為 null 的,如果你需要將某些字段的值更新為 null,有幾種方法可以實(shí)現(xiàn),本文將介紹幾種常見(jiàn)的方法2024-11-11
基于SpringBoot+Redis實(shí)現(xiàn)分布式鎖
本文主要介紹了基于SpringBoot+Redis實(shí)現(xiàn)分布式鎖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
Java使用Semaphore對(duì)單接口進(jìn)行限流
本篇主要講如何使用Semaphore對(duì)單接口進(jìn)行限流,主要有三種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包代碼示例
在Java應(yīng)用程序中有時(shí)我們需要從多個(gè)URL地址下載文件,并將這些文件打包成一個(gè)Zip文件進(jìn)行批量處理或傳輸,這篇文章主要給大家介紹了關(guān)于java批量下載將多個(gè)文件(minio中存儲(chǔ))壓縮成一個(gè)zip包的相關(guān)資料,需要的朋友可以參考下2023-11-11
Java Stream.reduce()方法操作實(shí)際案例講解
reduce是Java Stream API中的一個(gè)核心操作,用于將流中的元素組合起來(lái)產(chǎn)生單個(gè)結(jié)果,這篇文章主要介紹了Java Stream.reduce()方法操作實(shí)際案例講解,需要的朋友可以參考下2025-05-05
Spring實(shí)戰(zhàn)之獲取方法返回值操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之獲取方法返回值操作,涉及spring配置文件與方法返回值操作相關(guān)使用技巧,需要的朋友可以參考下2019-12-12

