简体   繁体   English

如何在Spring Boot和Spring WebFlux中使用“功能bean定义Kotlin DSL”?

[英]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.

相关问题 Kotlin在Spring bean定义dsl中的评估顺序? - Order of evaluation in Spring bean definition dsl for kotlin? 如何使用 Spring Boot Security 修复 Spring Boot Webflux 应用程序中的“名称为 requestMappingHandlerAdapter 的无效 bean 定义”错误 - How to fix 'Invalid bean definition with name requestMappingHandlerAdapter' error in Spring Boot Webflux app with Spring Boot Security 访问 Spring 功能 bean 定义 DSL 内部的 InjectionPoint - Accessing InjectionPoint inside Spring functional bean definition DSL 如何将@Value属性注入使用Spring 5和Kotlin Bean Definition DSL定义的bean - How to inject @Value properties into beans defined using Spring 5 and Kotlin Bean Definition DSL 如何使用Spring 5和Kotlin访问Bean Definition DSL的bean元素中的环境属性 - How to access Environment properties in the beans element of the Bean Definition DSL using Spring 5 and Kotlin Spring Boot-bean定义 - Spring Boot - bean definition 功能 bean 定义 Kotlin DSL - 将初始化程序添加到单个测试 class? - Functional bean definition Kotlin DSL - add initializer to single test class? 如何将 Spring HATEOAS "linkTo" 与 Kotlin DSL 一起使用? - How to use Spring HATEOAS "linkTo" with Kotlin DSL? 如何在 Spring Boot 中将 Query DSL 与 MongoDB 一起使用 - How to use Query DSL with MongoDB in Spring Boot Spring Kotlin Bean DSL - 仅当存在其他 bean 时才注册 bean? - Spring Kotlin Bean DSL - register bean only if other bean is present?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM