如何解决无法通过使用 kotlin 开放类构造函数实例化 spring bean 属性
open class XService( // this is open because spring-cglib
val yService: YService
) {
fun doX() {
yService.doY()
}
}
interface YService {
//method deFinitions
}
从上面可以看出,XService
依赖于 YService
接口。我试图通过提供 YService
接口的两个不同实现来实例化两个不同的 bean,比如说 AService:YService
和 BService:YService
。
@Configuration
class SomeBeans{
@Bean
fun firstXService(aService:AService):XService {//AService is a @Service annotated class,so it is perfectly being injected here,aka argument is not null
return XService(
yService = aService
)
}
@Bean
fun secondXService(bService:BService):XService {//BService is a @Service annotated class,aka argument is not null
return XService(
yService = bService
)
}
}
上面是完美实例化bean,可以看到这两个bean都位于上下文中。
但是当我碰巧运行一些集成测试(使用 spring 上下文)时,每次调用 xService.doX()
都会失败并显示 NPE:null
,这是因为 yService
为 null。
知道这里发生了什么吗?引擎盖下发生了什么?
提前致谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。