JavaFX, Kotlin and Spring Framework – Complete

github repository

This blog post fleshes out a previous, much briefer post on adding Spring Framework to the JavaFX and Kotlin mix. I took my original multi-controller application and converted all the JavaFX controllers into Spring Beans.  All the beans are initialised in a configuration class, and allowed me to refactor the controllers to use constructor based injection.

beans.png

Spring Controller Initialisation

All the controllers are created by Spring in a configuration class. Also the EventBus is also a bean and injected into all the controllers.

@Configuration
open class ApplicationConfig {
@Bean
open fun applicationController(fileListController: FileListController,
filePropertiesController: FilePropertiesController,
fileDataController: FileDataController): ApplicationController {
return ApplicationController(fileListController, fileDataController, filePropertiesController)
}

@Bean
open fun eventBus(): EventBus {
return EventBus()
}

@Bean
open fun fileDataController(eventBus: EventBus): FileDataController {
return FileDataController(eventBus)
}

@Bean
open fun fileListController(eventBus: EventBus): FileListController {
return FileListController(eventBus)
}

@Bean
open fun filePropertiesController(eventBus: EventBus): FilePropertiesController {
return FilePropertiesController(eventBus)
}
}

So the Application Controller is now much simpler, taking all the other controllers as constructor arguments.

class ApplicationController(private val fileListController: FileListController, private val fileDataController: FileDataController, private val filePropertiesController: FilePropertiesController) {
...
}

Application Startup

Application startup now needs to create a Spring Application Context and inject the application controller into the FXML Loader.

class Main : Application() {
private val context: AnnotationConfigApplicationContext = AnnotationConfigApplicationContext(ApplicationConfig::class.java)

@Throws(Exception::class)
override fun start(primaryStage: Stage) {
val controller = context.getBean("applicationController") as ApplicationController
val loader = FXMLLoader(javaClass.getResource("/springkotlin.fxml"))
loader.setController(controller)
...
}
}

And that’s it! The full source code is here.

2 thoughts on “JavaFX, Kotlin and Spring Framework – Complete

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s