简体   繁体   English

使用@Autowired 和@Component 时出现lateinit 属性未初始化错误

[英]Got lateinit property has not been initialized error when using @Autowired and @Component

This AwsSqsUtil class has a Component annotation, when I want to use it in my main function, I got error "kotlin.UninitializedPropertyAccessException: lateinit property awsSqsUtil has not been initialized, how to fix this issue?这个 AwsSqsUtil class 有一个组件注释,当我想在我的主 function 中使用它时,我收到错误“kotlin.UninitializedPropertyAccessException:lateinit 属性 awsSqsUtil 尚未初始化,如何解决此问题?

" "

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

awsSqsUtil.sendMessageToSqs()

Where exactly is your variable declaration?你的变量声明到底在哪里? Is it inside a class that is a component/service/controller/etc, or just on top level?它是在作为组件/服务/控制器/等的 class 内,还是仅在顶层?

I expect your answer to be top lever or a normal class, since that is when you'd usually encounter this.我希望您的回答是顶级杠杆或普通的 class,因为那是您通常会遇到这种情况的时候。

And the solution would be to leave the main class alone in a spring boot app.解决方案是将主 class 单独留在 spring 引导应用程序中。 If you want something to run at start-up, you can create a fun (inside a proper spring container) that you annotate with @PostConstruct .如果您想在启动时运行某些东西,您可以创建一个使用@PostConstruct注释的乐趣(在适当的 spring 容器内)。 This way it will run after the spring app has actually started, and you'll be able to autowire at will.这样它将在 spring 应用程序实际启动后运行,您将能够随意自动装配。

暂无
暂无

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

相关问题 @Autowired 但得到了 lateinit 属性 testBean has not been initialized 错误 - @Autowired but got lateinit property testBean has not been initialized error “lateinit 属性<varname>在 Kotlin 上使用带有 SpringBootTest 的 WebTestClient 时尚未初始化”</varname> - “lateinit property <varName> has not been initialized” when using WebTestClient with SpringBootTest on Kotlin spring::lateinit 属性 userRepo 尚未初始化 - spring:: lateinit property userRepo has not been initialized 从`.properties`文件中检索值| lateinit属性尚未初始化 - Retrieve values from `.properties` file | lateinit property 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 Spring thymeleaf TemplateEngine 出现错误 第二次创建pdf时模板引擎已经初始化 - Spring thymeleaf TemplateEngine Getting error Template engine has already been initialized when creating a pdf the second time Spring io @Autowired:空白的最终字段可能尚未初始化 - Spring io @Autowired: The blank final field may not have been initialized 调试时未使用自动装配初始化 MapStruct 映射器 - MapStruct mapper not initialized with autowired when debug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM