Spring Boot自动配置模块中@ConfigurationProperties构造函数绑定失效
Spring Boot 3.1.2 Kotlin配置属性构造函数绑定失效问题
使用Spring Boot v3.1.2(基于spring-boot-starter-parent,Kotlin语言)开发自研自动配置模块时,遇到@ConfigurationProperties构造函数绑定失效的问题:
- 当配置类与主类在同一模块时,以下不可变属性(val)的data class可正常通过构造函数绑定配置:
@ConfigurationProperties("eventsource.streams.aggregate") data class KafkaStreamsProperties( val bootstrapServers: String, val applicationId: String, val eventTopic: String, val targetTopic: String )
- 将该配置类迁移到仅包含以下依赖的独立Maven库模块后:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency>
启动时抛出错误:Parameter 0 of constructor in no.holden.catalogs.libs.event.aggregate.KafkaStreamsProperties required a bean of type 'java.lang.String' that could not be found.
- 若将配置类改为可变属性(var)+ 默认值的形式,配置绑定恢复正常:
@ConfigurationProperties("eventsource.streams.aggregate") data class KafkaStreamsProperties( var bootstrapServers: String = "", var applicationId: String = "", var eventTopic: String = "", var targetTopic: String = "" )
已确认org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中已配置no.holden.catalogs.libs.event.aggregate.KafkaStreamsProperties。
内容的提问来源于stack exchange,提问作者Bent Holden
相关产品推荐
相关产品推荐

