亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

@ConfigurationProperties 構造函數綁定在 2.2.0.RC1 中不起作用

@ConfigurationProperties 構造函數綁定在 2.2.0.RC1 中不起作用

米琪卡哇伊 2023-09-27 17:33:09
我正在嘗試新的 Spring Boot 2.2.0.RC1 版本,特別是第 2.8.2 節中描述的新配置屬性構造函數綁定功能。構造函數綁定。我建立了一個非常小的項目,其中有一個類,如下所示:@Configuration@ConfigurationProperties("acme")public class AppConfig {    private final String stuff;    public AppConfig(String stuff) {        this.stuff = stuff;    }    public String getStuff() {        return stuff;    }}和這樣的 application.yml :server:  port: 9000acme:  stuff: hello there我的主要方法在這個類中:@SpringBootApplicationpublic class AcmeApplication {    public static void main(String[] args) {        new SpringApplicationBuilder(AcmeApplication.class)                .logStartupInfo(true)                .bannerMode(Banner.Mode.CONSOLE)                .web(WebApplicationType.SERVLET)                .run();    }}運行應用程序的結果是這樣的輸出:***************************APPLICATION FAILED TO START***************************Description:Parameter 0 of constructor in com.acme.config.AppConfig required a bean of type 'java.lang.String' that could not be found.Action:Consider defining a bean of type 'java.lang.String' in your configuration.有趣的是,如果我更改 AppConfig 類中的代碼以使用屬性綁定,通過刪除構造函數、從“stuff”字段中刪除“final”修飾符并添加 setStuff(String) 方法,應用程序將正常啟動(setStuff 方法)按預期被調用)。在嘗試讓構造函數綁定工作時我缺少什么?我嘗試刪除 @Configuration 注釋,添加 @EnableConfigurationProperties 注釋,添加 @ConfigurationPropertiesScan 等等,但從閱讀文檔來看,這些東西似乎都不適用于此。在我看來,它試圖注入 Spring bean,而不是構建和注入配置屬性對象。這就是為什么我認為刪除 @Configuration 注釋可能會有所幫助,但沒有什么區別。順便說一句,我希望這個 AppConfig 是一個 Spring Bean,這樣我就可以將它注入到 Service 類中。
查看完整描述

1 回答

?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

我錯誤地查看了里程碑文檔。更新的 RC1 文檔顯示我需要使用 @ImmutableConfigurationProperties 注釋以及 @ConfigurationPropertiesScan 注釋。


查看完整回答
反對 回復 2023-09-27
?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

進行了簡短的研究。這似乎是原因https://github.com/spring-projects/spring-boot/issues/16928#issuecomment-494717209 請查看整個線程以獲取更多上下文。這是前面提到的問題描述https://github.com/spring-projects/spring-boot/issues/8762#issuecomment-494310988


查看完整回答
反對 回復 2023-09-27
  • 1 回答
  • 0 關注
  • 111 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號