1
votes

I'm trying to lazily initialize a bean in a spring boot app with Kotlin.

I have a spring @Configuration file where i have a lazily annotated 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 @RequestMappings

println(something.thing)

I've added logs in the @Bean method and confirmed that the bean isn't getting eagerly initialized. 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. I don't know what i'm doing wrong.

Here are the versions:

Java: 1.8.0_191

Kotlin: 1.1.60

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. Appreciate the help.

1
You are using several years old Kotlin. It could be a good idea to update to 1.3.21 for example - Eugene Petrenko

1 Answers

0
votes

Update your Kotlin compiler. @Lazy works fine for me in Kotlin v1.3.x.