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
。
答案 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
}