[英]How to use “Functional bean definition Kotlin DSL” with Spring Boot and Spring WebFlux?
At 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". 在https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt处 ,注释显示了如何定义Spring Beans通过新的“功能bean定义Kotlin DSL”。 I also found https://github.com/sdeleuze/spring-kotlin-functional . 我还发现了https://github.com/sdeleuze/spring-kotlin-functional 。 However, this example uses just plain Spring and not Spring Boot . 但是,此示例仅使用普通 Spring而不使用Spring Boot 。 Any hint how to use the DSL together with Spring Boot is appreciated. 任何有关如何将DSL与Spring Boot一起使用的提示均表示赞赏。
Spring Boot is based on Java Config, but should allow experimental support of user-defined functional bean declaration DSL via ApplicationContextInitializer
support as described here . 春天引导是基于Java的配置,但应该允许试验支持用户自定义的功能bean声明DSL通过ApplicationContextInitializer
支持,描述在这里 。
In practice, you should be able to declare your beans for example in a Beans.kt
file containing a beans()
function. 实际上,您应该能够在包含beans()
函数的Beans.kt
文件中声明您的bean。
fun beans() = beans {
// Define your bean with Kotlin DSL here
}
Then in order to make it taken in account by Boot when running main()
and tests, create an ApplicationContextInitializer
class as following: 然后,为了使Boot在运行main()
和测试时将其考虑在内,请创建一个ApplicationContextInitializer
类,如下所示:
class BeansInitializer : ApplicationContextInitializer<GenericApplicationContext> {
override fun initialize(context: GenericApplicationContext) =
beans().initialize(context)
}
And ultimately, declare this initializer in your application.properties
file: 最后,在您的application.properties
文件中声明此初始化application.properties
:
context.initializer.classes=com.example.BeansInitializer
You will find a full example here and can also follow this issue about dedicated Spring Boot support for functional bean registration. 您可以在此处找到完整的示例,也可以关注有关针对功能性bean注册的专用Spring Boot支持的此问题 。
Another way to do it in Spring Boot would be : 在Spring Boot中执行此操作的另一种方法是:
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args) {
addInitializers(
beans {
// Define your bean with Kotlin DSL here
}
)
}
}
Maybe this would be more transparent 也许这会更加透明
fun beans() = beans {
// Define your bean with Kotlin DSL here
}
@Configuration
class KotlinBeanDSLConfig {
@Autowired
lateinit var applicationContext: GenericApplicationContext
@PostConstruct
fun initKotlinBeans() {
beans().initialize(applicationContext)
}
}
You can define your beans in *Config.kt file and implement initalize method of ApplicationContextInitializer interface. 您可以在* Config.kt文件中定义bean,并实现ApplicationContextInitializer接口的initalize方法。
override fun initialize(applicationContext: GenericApplicationContext) {
....
}
Some bean definition here. 一些bean的定义在这里。
bean<XServiceImpl>("xService")
bean("beanName") {
BeanConstructor(ref("refBeanName"))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.