在spring kotlin支持中指定默认autowireMode的方法

时间:2018-01-07 04:45:47

标签: spring-boot kotlin

Spring boot version 2,spring framework version 5。

如何为Bean DSL指定默认autowireMode?我目前对每个bean使用它:

fun beans() = org.springframework.context.support.beans {
    bean<BeanImpl>(autowireMode = BeanDefinitionDsl.Autowire.BY_NAME)
    bean<AnotherBeanImpl>(autowireMode = BeanDefinitionDsl.Autowire.BY_NAME)
    // hundreds more bean
}

这很乏味。 bean实现来自遗留应用程序,因此我无法使用Component

1 个答案:

答案 0 :(得分:2)

只需编写自己的扩展方法:

inline fun <reified T : Any> BeanDefinitionDsl.beanByName(name: String? = null,
                                          scope: Scope? = null,
                                          isLazyInit: Boolean? = null,
                                          isPrimary: Boolean? = null
                                          isAutowireCandidate: Boolean? = null) {
  this.bean(name = name, scope = scope, isLazyInit = isLazyInit, isPrimary = isPrimary, isAutowireCandidate = isAutowireCandidate, autowireMode = Autowire.BY_NAME)
}

然后使用它

fun beans() = org.springframework.context.support.beans {
    beanByName<BeanImpl>()
    beanByName<AnotherBeanImpl>()
    // hundreds more bean
}