Menu

Let Spring Be Your JavaFX Controller Factory

21 February 2017

One of the best things to come out of the Spring ecosystem is Spring Boot. I use it quite a bit for web development (when Grails isn't the best fit). Recently I was playing around with JavaFX and I wondered if there was a way to use Spring with JavaFX; specifically I really wanted to take advantage of Spring's dependency injection and Spring Data. Turns out, it is pretty simple.

Like most of my Spring Boot applications, I started by going to https://start.spring.io/ and adding JPA, Validation and H2 as dependencies. I then generated a Gradle project for Spring Boot 1.5.1. You can name the Group and Artifact whatever you want and use Maven if you prefer.

What you'll have now is a standard Spring Boot application with a Gradle build. Since JavaFX is part of Java 8, there's no additional dependencies to add. The main change required is how the Spring Boot Application boots up along side the JavaFX bits.

The changes need to be made to the Spring Boot Application entry point; in my case it was com.example.DemoApplication. Here's the full file and then I'll explain it afterwards:

First, we need to extend JavaFX's Application class. Then in the init() method we need to get an instance of the ConfigurableApplicationContext. Generally, a Spring Boot application simply has SpringApplication.run(DemoApplication.class); and we don't care about the context, but it does return a ConfigurableApplicationContext and we can utilize that.

We go ahead and create the initial FXMLLoader from an entry point JavaFX fxml resource then the magic happens. We have to tell the FXMLLoader that we want Spring to be the controller factory.

When JavaFX's launch(DemoApplication.class, args); is called, the init method kicks off Spring Boot and wires up all the beans. And that's it! You can now start adding whatever additional Spring functionality you might need. In my use case, I needed a pretty simple way of managing a local data store for offline mode. Spring Data made that a piece of cake.