[英]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.