【问题标题】:Got lateinit property has not been initialized error when using @Autowired and @Component使用@Autowired 和@Component 时出现lateinit 属性未初始化错误
【发布时间】:2021-06-09 21:03:59
【问题描述】:

这个 AwsSqsUtil 类有一个 Component 注释,当我想在我的主函数中使用它时,我收到错误“kotlin.UninitializedPropertyAccessException: lateinit property awsSqsUtil has not been initialized, 如何解决这个问题?

"

@Component
class AwsSqsUtil {
   fun sendMessageToSqs() {
  }
}
@Autowired
private lateinit var awsSqsUtil: AwsSqsUtil

awsSqsUtil.sendMessageToSqs()

【问题讨论】:

    标签: spring kotlin


    【解决方案1】:

    你的变量声明到底在哪里?它是在作为组件/服务/控制器/等的类中,还是仅在顶层?

    我希望您的回答是顶级或普通班,因为那是您通常会遇到这种情况的时候。

    解决方案是将主类单独留在 Spring Boot 应用程序中。如果您想在启动时运行某些东西,您可以创建一个有趣的(在适当的弹簧容器内),并使用@PostConstruct 进行注释。这样它会在 spring 应用真正启动后运行,并且你可以随意自动装配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 2023-03-25
      相关资源
      最近更新 更多