小编H3n*_*npi的帖子

Spring Boot @Autowired 自定义 application.properties

我正在尝试让我的 custon application.properties 字段在 kotlin spring boot 应用程序中工作。

我的当前设置:

应用程序属性.kt:

@Configuration
@ConfigurationProperties("mt")
class ApplicationProperties {

    var initDatabase: Boolean? = null
    val kafka = Kafka()

    class Kafka {
        lateinit var host: String
        lateinit var port: String
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序属性:

mt.kafka.host=localhost
mt.kafka.port=9092
mt.init-database=true
Run Code Online (Sandbox Code Playgroud)

还有一个我想使用我的应用程序的类:InitApp.kt

@Component
@EnableTransactionManagement
@EnableConfigurationProperties(ApplicationProperties::class)
class InitApp {
    @Autowired
    lateinit var conf: ApplicationProperties

    @Bean
    fun createKafkaConsumer() {
        log.info("${conf.kafka.host}")
    }
}
Run Code Online (Sandbox Code Playgroud)

可悲的是,这不起作用,因为我得到了:

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property conf has not been initialized
Run Code Online (Sandbox Code Playgroud)

我遵循了 Sébastien Deleuze 和 Josh Long @ Spring …

spring kotlin spring-boot

5
推荐指数
1
解决办法
3473
查看次数

标签 统计

kotlin ×1

spring ×1

spring-boot ×1