亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot查看項(xiàng)目配置信息的幾種常見方法

 更新時間:2025年04月23日 09:19:39   作者:愛的嘆息  
這篇文章主要為大家詳細(xì)介紹了查看Spring Boot項(xiàng)目所有配置信息的幾種方法,包括 Actuator端點(diǎn),日志輸出,代碼級獲取等方式并附帶詳細(xì)步驟和示例,希望對大家有一定的幫助

以下是查看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 如何使用Velocity引擎生成代碼

    Java 如何使用Velocity引擎生成代碼

    代碼生成器,可以有效減少編寫重復(fù)代碼,快速實(shí)現(xiàn)簡單的業(yè)務(wù)邏輯,也能讓我們的代碼保持一致。那目前,我們看到的代碼生成器,大部分是基于velocity引擎模板生成的,接下來我們就學(xué)習(xí)一下如何實(shí)現(xiàn)代碼生成器。
    2021-06-06
  • springboot如何使用MybatisPlus

    springboot如何使用MybatisPlus

    MyBatisPlus是一個強(qiáng)大的數(shù)據(jù)庫操作框架,其代碼生成器可以快速生成實(shí)體類、映射文件等,本文介紹了如何導(dǎo)入MyBatisPlus相關(guān)依賴,創(chuàng)建代碼生成器,并配置數(shù)據(jù)庫信息以逆向生成代碼,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • SpringBoot攔截器的使用小結(jié)

    SpringBoot攔截器的使用小結(jié)

    今天給大家總結(jié)一下SpringBoot下攔截器的使用,需要的朋友參考下吧
    2017-05-05
  • java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼

    java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼

    java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼,需要的朋友可以參考一下
    2013-03-03
  • Spring MVC數(shù)據(jù)綁定概述及原理詳解

    Spring MVC數(shù)據(jù)綁定概述及原理詳解

    這篇文章主要介紹了Spring MVC數(shù)據(jù)綁定概述及原理詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • IDEA與JDK、Maven安裝配置完整步驟解析

    IDEA與JDK、Maven安裝配置完整步驟解析

    這篇文章主要介紹了如何安裝和配置IDE(IntelliJ?IDEA),包括IDE的安裝步驟、JDK的下載與配置、Maven的安裝與配置,以及如何在IDE中使用Maven進(jìn)行Java開發(fā),需要的朋友可以參考下
    2025-03-03
  • Spring Security架構(gòu)以及源碼詳析

    Spring Security架構(gòu)以及源碼詳析

    這篇文章主要給大家介紹了關(guān)于Spring Security架構(gòu)以及源碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫配置過程

    springboot 中 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)自動日志記錄的方式詳細(xì)介紹

    這篇文章主要介紹了使用Aop的方式實(shí)現(xiàn)自動日志記錄,通過監(jiān)聽器去監(jiān)聽,當(dāng)訪問到具體的類方法,通過aop切面去獲取訪問的方法,然后將日志記錄下來,就這種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Spring啟動流程源碼解析

    Spring啟動流程源碼解析

    這篇文章主要介紹了Spring啟動流程源碼解析,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07

最新評論