如何将 "Functional bean definition Kotlin DSL" 与 Spring Boot 和 Spring WebFlux 一起使用?
How to use "Functional bean definition Kotlin DSL" with Spring Boot and Spring WebFlux?
在 https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt the comment shows how to define Spring Beans via the new "Functional bean definition Kotlin DSL". I also found https://github.com/sdeleuze/spring-kotlin-functional。但是,此示例仅使用 plain Spring 而不是 Spring Boot。任何有关如何将 DSL 与 Spring Boot 一起使用的提示都将受到赞赏。
Spring 引导基于 Java 配置,但应该允许用户定义的实验性支持 functional bean declaration DSL via ApplicationContextInitializer
support as described .
实际上,您应该能够在包含 beans()
函数的 Beans.kt
文件中声明您的 bean。
fun beans() = beans {
// Define your bean with Kotlin DSL here
}
然后为了在运行 main()
和测试时被Boot考虑,创建一个ApplicationContextInitializer
class如下:
class BeansInitializer : ApplicationContextInitializer<GenericApplicationContext> {
override fun initialize(context: GenericApplicationContext) =
beans().initialize(context)
}
最后,在您的 application.properties
文件中声明此初始化程序:
context.initializer.classes=com.example.BeansInitializer
您将找到一个完整的示例 here and can also follow this issue 关于专用 Spring 功能 bean 注册的引导支持。
在 Spring 引导中执行此操作的另一种方法是:
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args) {
addInitializers(
beans {
// Define your bean with Kotlin DSL here
}
)
}
}
你可以在*Config.kt文件中定义你的bean并实现ApplicationContextInitializer接口的initalize方法。
override fun initialize(applicationContext: GenericApplicationContext) {
....
}
这里有一些 bean 定义。
bean<XServiceImpl>("xService")
bean("beanName") {
BeanConstructor(ref("refBeanName"))
}
在 https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt the comment shows how to define Spring Beans via the new "Functional bean definition Kotlin DSL". I also found https://github.com/sdeleuze/spring-kotlin-functional。但是,此示例仅使用 plain Spring 而不是 Spring Boot。任何有关如何将 DSL 与 Spring Boot 一起使用的提示都将受到赞赏。
Spring 引导基于 Java 配置,但应该允许用户定义的实验性支持 functional bean declaration DSL via ApplicationContextInitializer
support as described
实际上,您应该能够在包含 beans()
函数的 Beans.kt
文件中声明您的 bean。
fun beans() = beans {
// Define your bean with Kotlin DSL here
}
然后为了在运行 main()
和测试时被Boot考虑,创建一个ApplicationContextInitializer
class如下:
class BeansInitializer : ApplicationContextInitializer<GenericApplicationContext> {
override fun initialize(context: GenericApplicationContext) =
beans().initialize(context)
}
最后,在您的 application.properties
文件中声明此初始化程序:
context.initializer.classes=com.example.BeansInitializer
您将找到一个完整的示例 here and can also follow this issue 关于专用 Spring 功能 bean 注册的引导支持。
在 Spring 引导中执行此操作的另一种方法是:
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args) {
addInitializers(
beans {
// Define your bean with Kotlin DSL here
}
)
}
}
你可以在*Config.kt文件中定义你的bean并实现ApplicationContextInitializer接口的initalize方法。
override fun initialize(applicationContext: GenericApplicationContext) {
....
}
这里有一些 bean 定义。
bean<XServiceImpl>("xService")
bean("beanName") {
BeanConstructor(ref("refBeanName"))
}