我有以下一段代碼:
@Component
class TestClass: InitializingBean, DisposableBean {
@Autowired
private lateinit var testBean: SomeObject
override fun afterPropertiesSet(){
log.info("testBean 1: $testBdean")
}
fun testFunction(testName: String): Boolean {
log.info("testBean 2: $testBdean")
}
@Throws(Exception::class)
override fun destroy() {
}
}
第一個 testBean 1 成功了,但是 第二個 testBean 2 顯示錯誤: lateinit property testBean has not been initialized. 就是說 testBean 在 afterPropertiesSet() 已經初始化成功,但是其它函式不用用它?
有人建議我把testBean 放到 constructor里,比如: TestClass(testBean),這樣所有函式就可以用它了。但是因為有其他很多地方要用這個類,所以把testBean放到constructor不是很方便 (因為其他的類都有初始化testBean然后傳給TestClass的constructor)。
不知道有沒有其它辦法可以初始化這個testBean? 謝謝!
uj5u.com熱心網友回復:
代碼kotlin啊,頂uj5u.com熱心網友回復:
@Autowired是spring的??轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279738.html
標籤:Java SE
上一篇:大佬們,救救萌新吧
