I'm trying to use Spring DI for kotlin-javafx desktop app but Spring doesn't inject a beans to lateinit property.
Here is my starter class
package ui
@Component
class Starter : Application() {
override fun start(primaryStage: Stage?) {
val root : Parent = FXMLLoader.load(javaClass.getResource("/view/main.fxml"))
primaryStage?.title = "Title"
primaryStage?.scene = Scene(root)
primaryStage?.show()
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
AnnotationConfigApplicationContext(SpringConfig::class.java)
launch(Starter::class.java, *args)
}
}
}
Here is my Spring-config class
package config
@Configuration
@ComponentScan(basePackages = arrayOf("domain", "ui"))
open class SpringConfig {
}
Here is my bean that I want to inject
package domain
@Component
open class State {
private val coinsState = mapOf(Coin.SYS to CoinState(Coin.SYS))
fun setActiveForCoin(coin : Coin, isActive : Boolean) {
val coin = coinsState[coin]
if (coin == null) throw IllegalArgumentException("Coin $coin does not exist!")
coin.isActive = isActive
}
}
and finally my javafx controller which should receive a bean (instance of the controller created automatically, I just setup his name in .fxml file)
package ui.controller
@Component
class CoinController {
@Autowired
private lateinit var state : State
@FXML
fun showConfirmDialog(actionEvent: ActionEvent) {
println(state)
val alert = Alert(AlertType.CONFIRMATION)
alert.title = "Confirmation Dialog"
alert.headerText = "Look, a Confirmation Dialog"
//alert.contentText = "Are you ok with this?"
val result = alert.showAndWait()
if (result.get() == ButtonType.OK) {
// ... user chose OK
} else {
// ... user chose CANCEL or closed the dialog
}
}
}