我有一個非常普通的 Spring Boot v2.6.4 Java 應用程式,使用 Java 17。
我試圖讓記錄為我的@ConfigurationProperties 作業,但收效甚微。
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "custom")
@ConstructorBinding
public record CustomConfigProperties(String path) { }
這失敗了:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: @Configuration class 'CustomConfigProperties' may not be final. Remove the final modifier to continue.
顯然,記錄是隱含的最終結果,所以我很難過。再多的谷歌搜索也不會產生任何有用的東西。我能找到的最接近的是這個SO question,但它并不是真正直接相關的事實上,那里的評論者和我有同樣的問題)
uj5u.com熱心網友回復:
@Configuration應該用于通過@Bean方法定義 bean 的類。使用注釋@Configuration會觸發創建 CGLib 代理來攔截這些@Bean方法之間的呼叫。正是這種代理嘗試失敗了,因為該記錄是隱含的最終記錄。
您應該@Configuration從您的記錄中洗掉,然后使用或啟用您的@ConfigurationProperties記錄。您也可以洗掉,因為記錄是隱式建構式系結的。如果記錄具有多個建構式,并且您需要指示應該使用哪個建構式進行屬性系結,則只需要在記錄上使用。@ConfigurationPropertiesScan@EnableConfigurationProperties(CustomConfigProperties.class)@ConstructorBinding@ConstructorBinding
uj5u.com熱心網友回復:
如果我明確地注釋我的主應用程式類,它會起作用:
@ConfigurationPropertiesScan
這很荒謬,因為沒有這個注釋,一個普通的舊類風格就可以作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/447678.html
上一篇:連接到mysql時,用戶“root'@'localhost'”的訪問被拒絕
下一篇:如何使用建構式注入
