简体   繁体   English

Spring @Lazy注释不能与kotlin一起使用

[英]Spring @Lazy annotation not working with kotlin

I'm trying to lazily initialize a bean in a spring boot app with Kotlin. 我正试图在Kotlin的弹簧启动应用程序中懒洋洋地初始化一个bean。

I have a spring @Configuration file where i have a lazily annotated bean: 我有一个spring @Configuration文件,我有一个懒惰的注释bean:

@Lazy
@Bean
open fun createSomething(): Something {
    return Something("yo")
}

I have a rest controller where i have lazily autowired the bean 我有一个休息控制器,我懒得自动装豆

@Lazy
@Autowired
private lateinit var something: Something

I'm accessing a variable in the class in one of the @RequestMapping s 我正在其中一个@RequestMapping访问类中的变量

println(something.thing)

I've added logs in the @Bean method and confirmed that the bean isn't getting eagerly initialized. 我在@Bean方法中添加了日志,并确认bean没有得到急切的初始化。 The problem is the it's not getting initialized even on access. 问题是它即使在访问时也没有初始化。 The class is a simple open class. 该课程是一个简单的开放课程。

open class Something(val thing: String)

The bean initialization never happens. bean初始化永远不会发生。 I don't know what i'm doing wrong. 我不知道我做错了什么。

Here are the versions: 以下是版本:

Java: 1.8.0_191 Java:1.8.0_191

Kotlin: 1.1.60 科特林:1.1.60

Spring Boot: 2.0.2.RELEASE Spring Boot:2.0.2.RELEASE

I don't know if it's some sort of version problem or i'm doing something in using the lazy bean. 我不知道它是否是某种版本问题,或者我在使用懒惰的bean时正在做些什么。 Appreciate the help. 感谢帮助。

Update your Kotlin compiler. 更新您的Kotlin编译器。 @Lazy works fine for me in Kotlin v1.3.x. @Lazy在Kotlin v1.3.x中对我很好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM