简体   繁体   English

@Autowired 但得到了 lateinit 属性 testBean has not been initialized 错误

[英]@Autowired but got lateinit property testBean has not been initialized error

I have following class (partial code):我有以下 class (部分代码):

@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() {
        
    }
}

I saw testBean 1 was run successfully but testBean 2 gave error: lateinit property testBean has not been initialized.我看到 testBean 1 运行成功,但是 testBean 2 报错:lateinit 属性 testBean 没有被初始化。 So the testBean bean was initialized in afterPropertiesSet() and not available in other functions?那么 testBean bean 是在 afterPropertiesSet() 中初始化的,在其他函数中不可用? I know if we put testBean in the constructor TestClass(testBean) it will be initialized and available to all functions.我知道如果我们将 testBean 放在构造函数 TestClass(testBean) 中,它将被初始化并可供所有函数使用。 But is there another way because TestClass will be called from other packages and not every package can pass the testBean to the constructor.但是还有其他方法吗,因为 TestClass 将从其他包中调用,并不是每个 package 都可以将 testBean 传递给构造函数。

You could create an object that holds your TestClass and use that holder to refer to your create component您可以创建一个包含您的TestClassobject并使用该持有者来引用您的创建组件

something like:就像是:

@SpringBootApplication
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

@Component
class SomeObject(val name:String = "some object")

@Component
class TestClass(val someObject: SomeObject) {
    init {
        TestClassHolder.testClass = this
    }
    fun test() = someObject.name

}

object TestClassHolder {
    lateinit var testClass: TestClass
}

class NotBeanClass {
    fun call() = TestClassHolder.testClass.test()
}


@RestController
class TestController {

    @GetMapping
    fun test() = NotBeanClass().call()

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用@Autowired 和@Component 时出现lateinit 属性未初始化错误 - Got lateinit property has not been initialized error when using @Autowired and @Component spring::lateinit 属性 userRepo 尚未初始化 - spring:: lateinit property userRepo has not been initialized 带有 Spring DI 的 Kotlin:lateinit 属性尚未初始化 - Kotlin with Spring DI: lateinit property has not been initialized Kotlin + SpringBoot:DI lateinit属性服务尚未初始化 - Kotlin + SpringBoot : DI lateinit property service has not been initialized kotlin.UninitializedPropertyAccessException:lateinit 属性尚未初始化 - kotlin.UninitializedPropertyAccessException: lateinit property has not been initialized 从`.properties`文件中检索值| lateinit属性尚未初始化 - Retrieve values from `.properties` file | lateinit property has not been initialized “lateinit 属性<varname>在 Kotlin 上使用带有 SpringBootTest 的 WebTestClient 时尚未初始化”</varname> - “lateinit property <varName> has not been initialized” when using WebTestClient with SpringBootTest on Kotlin Kotlin 自动装配问题 - lateinit (obv) - Kotlin Autowired problems - lateinit (obv) Spring io @Autowired:空白的最终字段可能尚未初始化 - Spring io @Autowired: The blank final field may not have been initialized Spring thymeleaf TemplateEngine 出现错误 第二次创建pdf时模板引擎已经初始化 - Spring thymeleaf TemplateEngine Getting error Template engine has already been initialized when creating a pdf the second time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM