Closed
Description
here's the spring initializr configuration i used
here's some kotlin code
package com.example.nativekotlin
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.boot.runApplication
import org.springframework.context.ApplicationListener
import org.springframework.context.annotation.Bean
import org.springframework.context.support.beans
import org.springframework.data.annotation.Id
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Component
@SpringBootApplication
class NativeKotlinApplication {
/*@Bean
fun myListener(cr: CustomerRepository) = MyListener(cr)*/
}
fun main(args: Array<String>) {
runApplication<NativeKotlinApplication>(*args) {
this.addInitializers(beans {
bean {
MyListener(ref())
}
})
}
}
class MyListener(val repo: CustomerRepository) : ApplicationListener<ApplicationReadyEvent> {
override fun onApplicationEvent(event: ApplicationReadyEvent) {
listOf("James", "Josh")
.map { Customer(null, it) }
.map { repo.save(it) }
.forEach { println(it) }
}
}
interface CustomerRepository : CrudRepository<Customer, Int>
data class Customer(@Id val id: Int?, val name: String)
I compile it using mvn -Pnative -DskipTests clean package
to get a GraalVM binary. if I leave the code as-is, on the JRE, I see James
and Josh
. If I run it as a native image on GraalVM, I see James
, Josh
, James
, Josh
. (the same thing, twice). My listener is being called twice, for some reason.
Odder still, if I use @Bean
to register the MyListener
or if I use @Component
, then in those cases I only see Josh
, and James
once. Not twice.