最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

將SpringBoot屬性配置類@ConfigurationProperties注冊為Bean的操作方法

 更新時(shí)間:2025年12月24日 09:05:58   作者:李少兄  
在現(xiàn)代 Spring Boot 應(yīng)用開發(fā)中,外部化配置(Externalized Configuration)是實(shí)現(xiàn)應(yīng)用靈活性和可維護(hù)性的核心機(jī)制之一,然而,許多開發(fā)者在使用的時(shí)候經(jīng)常會遇到如何確保該配置類被正確注冊為 Spring 容器中的 Bean,下面小編為大家詳細(xì)說說

前言

在現(xiàn)代 Spring Boot 應(yīng)用開發(fā)中,外部化配置(Externalized Configuration)是實(shí)現(xiàn)應(yīng)用靈活性和可維護(hù)性的核心機(jī)制之一。Spring Boot 提供了強(qiáng)大的 @ConfigurationProperties 注解,用于將配置文件(如 application.ymlapplication.properties)中的屬性值類型安全地綁定到 Java 對象中。

然而,許多開發(fā)者在使用 @ConfigurationProperties 時(shí)常常遇到一個(gè)關(guān)鍵問題:如何確保該配置類被正確注冊為 Spring 容器中的 Bean?

一、為什么需要將配置類注冊為 Bean?

@ConfigurationProperties 本身只是一個(gè)元數(shù)據(jù)注解,它告訴 Spring:“這個(gè)類用于綁定配置屬性”。但它不會自動(dòng)將該類注冊為 Spring Bean

只有當(dāng)該類成為 Spring 容器管理的 Bean 后,才能:

  • 被其他組件(如 Service、Controller)通過 @Autowired 注入使用;
  • 參與 Spring 的生命周期管理(如 @PostConstruct、InitializingBean);
  • 支持配置屬性的驗(yàn)證(如 @Validated + JSR-303 注解);
  • 在 Actuator 的 /actuator/configprops 端點(diǎn)中被監(jiān)控和展示。

因此,顯式或隱式地將配置類注冊為 Bean 是使用 @ConfigurationProperties 的前提條件。

二、注冊方式一:使用@Component(基于組件掃描)

2.1 基本用法

在配置類上同時(shí)標(biāo)注 @Component@ConfigurationProperties

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app.user")
public class UserProperties {
    private String name;
    private int age;

    // 必須提供 setter 方法(或使用 Lombok)
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // getter 方法(用于外部訪問)
    public String getName() { return name; }
    public int getAge() { return age; }
}

2.2 工作原理

  • @Component 是 Spring 的通用組件注解,會被 @ComponentScan 掃描并注冊為 Bean。
  • Spring Boot 在啟動(dòng)時(shí)會自動(dòng)處理所有帶有 @ConfigurationProperties 的 Bean,執(zhí)行屬性綁定(Property Binding)。
  • 因此,該類既是一個(gè)普通 Bean,又具備配置綁定能力。

2.3 使用要求

  • 包路徑必須被組件掃描覆蓋:通常主啟動(dòng)類(帶 @SpringBootApplication)所在包及其子包會被自動(dòng)掃描。若配置類位于其他包,需顯式配置 @ComponentScan(basePackages = "...")
  • 必須提供公共的 setter 方法:Spring 使用 JavaBean 規(guī)范進(jìn)行屬性注入,因此字段需有對應(yīng)的 setXxx() 方法(或使用 Lombok 的 @Setter)。

2.4 優(yōu)缺點(diǎn)分析

優(yōu)點(diǎn)缺點(diǎn)
代碼簡潔,直觀易懂依賴組件掃描機(jī)制,耦合度較高
適合小型項(xiàng)目或內(nèi)部模塊若包路徑未被掃描,容易導(dǎo)致 Bean 未注冊(靜默失?。?/td>
無需額外配置類不利于模塊化設(shè)計(jì)(尤其在多模塊項(xiàng)目中)

三、注冊方式二:使用@EnableConfigurationProperties(推薦方式)

3.1 基本用法

步驟 1:定義純配置類(不加 @Component):

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "app.user")
public class UserProperties {
    private String name;
    private int age;

    // setter / getter ...
}

步驟 2:在任意配置類(或主啟動(dòng)類)上啟用該配置:

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(UserProperties.class)
public class AppConfig {
    // 可以包含其他 @Bean 定義
}

或者直接在主啟動(dòng)類上啟用:

@SpringBootApplication
@EnableConfigurationProperties(UserProperties.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3.2 工作原理

  • @EnableConfigurationProperties 是一個(gè)復(fù)合注解,其核心作用是:
    1. 向 Spring 容器注冊一個(gè) ConfigurationPropertiesBeanRegistrar;
    2. 該 Registrar 會為指定的類(如 UserProperties.class動(dòng)態(tài)注冊一個(gè)名為 userProperties 的 Bean(默認(rèn)按類名首字母小寫);
    3. 同時(shí)觸發(fā) ConfigurationPropertiesBindingPostProcessor 對該 Bean 進(jìn)行屬性綁定。

技術(shù)細(xì)節(jié):注冊的 Bean 類型為 UserProperties,作用域?yàn)?singleton,且會自動(dòng)應(yīng)用 @Validated(如果類上有該注解)。

3.3 支持批量注冊

@EnableConfigurationProperties 接受多個(gè)類:

@EnableConfigurationProperties({
    UserProperties.class,
    DatabaseProperties.class,
    CacheProperties.class
})

3.4 優(yōu)缺點(diǎn)分析

優(yōu)點(diǎn)缺點(diǎn)
顯式聲明依賴,語義清晰需要額外編寫配置類(但通??珊喜⒌浆F(xiàn)有配置中)
不依賴組件掃描,適用于任何包路徑初學(xué)者可能不熟悉該注解
更符合“配置驅(qū)動(dòng)”設(shè)計(jì)理念
便于測試和模塊化(如 Starter 自動(dòng)配置)

官方推薦:Spring Boot 官方文檔和 Spring Cloud 項(xiàng)目普遍采用此方式。

四、最佳實(shí)踐

4.1 啟用配置屬性處理器(IDE 支持)

pom.xml 中添加以下依賴,可在 IDE 中獲得配置提示和校驗(yàn):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

編譯后會在 META-INF/spring-configuration-metadata.json 生成元數(shù)據(jù)文件。

4.2 配置屬性驗(yàn)證

結(jié)合 @Validated 和 JSR-303 注解實(shí)現(xiàn)輸入校驗(yàn):

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Min;
import org.springframework.validation.annotation.Validated;

@Validated
@ConfigurationProperties(prefix = "app.user")
public class UserProperties {
    @NotBlank
    private String name;

    @Min(0)
    private int age;

    // setters/getters
}

注意:驗(yàn)證功能僅在配置類被注冊為 Bean 后才生效。

4.3 構(gòu)造函數(shù)綁定(Immutable 配置)

Spring Boot 2.2+ 支持通過構(gòu)造函數(shù)綁定,實(shí)現(xiàn)不可變配置對象:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

@ConstructorBinding
@ConfigurationProperties(prefix = "app.user")
public class UserProperties {
    private final String name;
    private final int age;

    public UserProperties(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // only getters
}

此時(shí)無需 setter 方法,但必須使用 @EnableConfigurationProperties 注冊(@Component 方式不支持構(gòu)造函數(shù)綁定)。

4.4 配置屬性掃描(@ConfigurationPropertiesScan)

Spring Boot 2.2+ 引入了 @ConfigurationPropertiesScan,可自動(dòng)掃描指定包下的所有 @ConfigurationProperties 類并注冊為 Bean:

@SpringBootApplication
@ConfigurationPropertiesScan("com.example.config")
public class Application {}

注意:此方式仍要求配置類不能有 @Component,否則會重復(fù)注冊。

五、常見誤區(qū)與排查建議

誤區(qū) 1:只加@ConfigurationProperties,不注冊為 Bean

// 錯(cuò)誤!不會被注冊為 Bean
@ConfigurationProperties(prefix = "app.user")
public class UserProperties { ... }

后果:無法注入,/actuator/configprops 中看不到,配置無效。

解決:必須配合 @Component@EnableConfigurationProperties。

誤區(qū) 2:在@Component類上使用構(gòu)造函數(shù)綁定

@Component
@ConstructorBinding
@ConfigurationProperties(prefix = "app.user")
public class UserProperties { ... } // ? 不支持

原因@Component 由常規(guī)組件掃描注冊,不經(jīng)過 ConfigurationPropertiesBeanRegistrar,無法處理構(gòu)造函數(shù)綁定。

解決:改用 @EnableConfigurationProperties。

誤區(qū) 3:setter 方法非 public 或命名不規(guī)范

void setName(String name) { ... } // 包級私有,Spring 無法調(diào)用

后果:屬性綁定失敗,值為 null 或默認(rèn)值。

解決:確保 setter 為 public,且符合 JavaBean 規(guī)范(setPropertyName)。

排查技巧

  1. 啟動(dòng)時(shí)查看日志:Bound configuration properties: ...
  2. 訪問 Actuator 端點(diǎn):GET /actuator/configprops 查看是否注冊成功。
  3. 使用 ApplicationContext 手動(dòng)獲取 Bean 測試:
@Autowired
ApplicationContext ctx;

@PostConstruct
void check() {
    UserProperties props = ctx.getBean(UserProperties.class);
    System.out.println(props.getName());
}

六、總結(jié)與建議

場景推薦方式
小型項(xiàng)目、快速原型@Component + @ConfigurationProperties
中大型項(xiàng)目、模塊化架構(gòu)、Starter 開發(fā)@EnableConfigurationProperties
需要不可變配置(Immutable)@EnableConfigurationProperties + @ConstructorBinding
多個(gè)配置類集中管理@EnableConfigurationProperties({A.class, B.class})@ConfigurationPropertiesScan

終極建議優(yōu)先使用 @EnableConfigurationProperties。它解耦了配置類與組件掃描機(jī)制,語義明確,符合 Spring Boot 的設(shè)計(jì)哲學(xué),是企業(yè)級應(yīng)用的標(biāo)準(zhǔn)做法。

以上就是將SpringBoot屬性配置類@ConfigurationProperties注冊為Bean的操作方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot屬性配置類注冊為Bean的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • IDEA源碼修改器JarEditor使用(反編譯-打包一步到位)

    IDEA源碼修改器JarEditor使用(反編譯-打包一步到位)

    JarEditor是一個(gè)IDEA插件,用于修改jar包中的類文件,它允許用戶在不解壓jar包的情況下,直接在IDEA中編輯和修改類文件的源碼,修改完成后,可以一鍵編譯并生成新的jar包,替換原jar包
    2025-01-01
  • Java實(shí)現(xiàn)FTP文件上傳下載功能的詳細(xì)指南

    Java實(shí)現(xiàn)FTP文件上傳下載功能的詳細(xì)指南

    本文將詳細(xì)解釋如何用Java實(shí)現(xiàn)FTP協(xié)議下的文件上傳和下載功能,涵蓋連接設(shè)置、文件操作以及異常處理等方面,介紹了 java.net 和 org.apache.commons.net.ftp 庫,以及如何使用這些庫提供的工具和方法進(jìn)行文件傳輸,需要的朋友可以參考下
    2025-07-07
  • HttpClient 請求 URL字符集轉(zhuǎn)碼問題

    HttpClient 請求 URL字符集轉(zhuǎn)碼問題

    這篇文章主要介紹了HttpClient 請求 URL字符集轉(zhuǎn)碼問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • JAVA對字符串進(jìn)行32位MD5加密的實(shí)踐

    JAVA對字符串進(jìn)行32位MD5加密的實(shí)踐

    本文主要介紹了JAVA對字符串進(jìn)行32位MD5加密的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Spring Boot系列教程之日志配置

    Spring Boot系列教程之日志配置

    這篇文章主要給大家介紹了關(guān)于Spring Boot系列教程之日志配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • dubbo服務(wù)整合zipkin詳解

    dubbo服務(wù)整合zipkin詳解

    這篇文章主要介紹了dubbo服務(wù)整合zipkin,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • 關(guān)于BigDecimal類型之間比較問題

    關(guān)于BigDecimal類型之間比較問題

    這篇文章主要介紹了關(guān)于BigDecimal類型之間比較問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java使用Statement接口執(zhí)行SQL語句操作實(shí)例分析

    Java使用Statement接口執(zhí)行SQL語句操作實(shí)例分析

    這篇文章主要介紹了Java使用Statement接口執(zhí)行SQL語句操作,結(jié)合實(shí)例形式詳細(xì)分析了Java使用Statement接口針對mysql數(shù)據(jù)庫進(jìn)行連接與執(zhí)行SQL語句增刪改查等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-07-07
  • Maven?依賴沖突調(diào)解與版本控制問題記錄

    Maven?依賴沖突調(diào)解與版本控制問題記錄

    這篇文章主要介紹了Maven?依賴沖突調(diào)解與版本控制問題記錄,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-04-04
  • Java編程實(shí)現(xiàn)獲取當(dāng)前代碼行行號的方法示例

    Java編程實(shí)現(xiàn)獲取當(dāng)前代碼行行號的方法示例

    這篇文章主要介紹了Java編程實(shí)現(xiàn)獲取當(dāng)前代碼行行號的方法,結(jié)合實(shí)例形式分析了java基于StackTraceElement對象實(shí)現(xiàn)獲取代碼行號的相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08

最新評論

安塞县| 铜鼓县| 三穗县| 界首市| 郓城县| 拉孜县| 赤峰市| 阳谷县| 济阳县| 精河县| 台东市| 七台河市| 东兴市| 汤阴县| 黄梅县| 宜城市| 龙山县| 绥棱县| 平山县| 桂东县| 寻乌县| 汉寿县| 三都| 天祝| 宁乡县| 靖江市| 同仁县| 岑溪市| 库车县| 柳江县| 忻州市| 南丰县| 米脂县| 大冶市| 泸水县| 河曲县| 景德镇市| 昌黎县| 奉新县| 会昌县| 宜昌市|