SpringBoot查看項(xiàng)目配置信息的幾種常見方法
以下是查看Spring Boot項(xiàng)目所有配置信息的幾種方法,包括 Actuator端點(diǎn)、日志輸出、代碼級獲取 等方式,附帶詳細(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 訪問配置信息
啟動應(yīng)用后,訪問:
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. 通過日志輸出配置信息
在日志中直接打印所有配置屬性。
步驟
2.1 配置日志級別
在application.yml中啟用配置屬性日志:
logging: level: org.springframework.boot.context.properties: DEBUG
2.2 啟動應(yīng)用
啟動應(yīng)用后,日志中會輸出所有配置屬性的加載信息,例如:
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ì)的輸出,可在啟動時添加參數(shù):
java -jar your-app.jar --show-config
此參數(shù)會輸出所有合并后的配置屬性(Spring Boot 2.3+支持)。
3. 通過代碼獲取配置信息
在代碼中注入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)); } }
訪問接口
訪問:
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 訪問端點(diǎ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)鍵配置對比表格
方法 | 適用場景 | 優(yōu)點(diǎn) | 缺點(diǎn) |
---|---|---|---|
Actuator /configprops | 開發(fā)/生產(chǎn)環(huán)境監(jiān)控 | 直接通過HTTP接口查看所有配置 | 需配置安全策略(避免暴露敏感信息) |
日志輸出 | 調(diào)試或啟動時快速查看 | 無侵入性,適合臨時調(diào)試 | 需手動解析日志內(nèi)容 |
代碼獲取 | 需要程序內(nèi)處理配置信息 | 靈活控制輸出格式 | 需編寫代碼 |
DevTools /env | 開發(fā)環(huán)境快速查詢 | 支持查詢單個屬性 | 需依賴DevTools模塊 |
@ConfigurationProperties | 需要綁定配置到Bean時 | 類型安全,符合Spring規(guī)范 | 需針對每個配置前綴編寫B(tài)ean |
7.注意事項(xiàng)
1.安全配置:
生產(chǎn)環(huán)境需限制Actuator端點(diǎn)訪問,例如:
management: endpoints: web: exposure: include: "health" security: enabled: true
2.敏感信息過濾:
避免暴露敏感配置(如密碼),可通過management.endpoints.web.cors.allowed-origins或安全策略控制訪問。
3.性能影響:
/configprops端點(diǎn)在配置復(fù)雜時可能返回大量數(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>
通過上述方法,可根據(jù)需求選擇最適合的配置查看方式。如需進(jìn)一步優(yōu)化或解決特定問題(如安全配置、日志過濾),可提供具體場景!
到此這篇關(guān)于SpringBoot查看項(xiàng)目配置信息的幾種常見方法的文章就介紹到這了,更多相關(guān)SpringBoot查看項(xiàng)目配置信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼
java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼,需要的朋友可以參考一下2013-03-03Spring MVC數(shù)據(jù)綁定概述及原理詳解
這篇文章主要介紹了Spring MVC數(shù)據(jù)綁定概述及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫配置過程
這篇文章主要介紹了springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫配置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08使用Aop的方式實(shí)現(xiàn)自動日志記錄的方式詳細(xì)介紹
這篇文章主要介紹了使用Aop的方式實(shí)現(xiàn)自動日志記錄,通過監(jiān)聽器去監(jiān)聽,當(dāng)訪問到具體的類方法,通過aop切面去獲取訪問的方法,然后將日志記錄下來,就這種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04