Reactive Spring Boot: Part 3: JavaFX Spring Boot Application

This tutorial is a series of videos outlining
a number of steps to build a full Spring Boot application featuring a Kotlin service back
end, a Java client and a JavaFX user interface. This video will show how to create a Spring Boot JavaFX Application, so that JavaFX can
take advantage of Spring features like dependency injection. For this video we’re going to re-use the client project we created for the previous step,
and add a new module to it. But we could create this as a new project
rather than a new module, the steps would be very similar. Let’s create a new module for this project. This is a Spring Boot application so we select
Spring Initializr from the options on the left. We’ve selected Java 13 as our JDK, but we’re
not using any recent features so feel free to use any version from 8 onwards. We enter the group, and we’ll call the artifact
stock-ui. We keep the defaults of a Maven Project with
Java and Jar packaging, but select Java 11. Let’s enter a helpful description for the
module, this is our third module so it helps us to keep clear in our mind what each module
is responsible for. We can optionally change the default package. Here we can select any of the Spring Boot Starters we need, but for this module we don’t
actually need anything. We can keep the default module name and location. IntelliJ IDEA downloads the created project from Spring Initializr and sets up the IDE
correctly. If we’re given the option to show the run
configurations in Services, we can select this. The services window is a slightly nicer and
more useful way to see our running services and can help us to manage microservice applications. As usual, Spring Boot has generated a default application class for us. We will need to change this in order to launch
a JavaFX application. Since this is a JavaFX application and not a web application, we can set the spring web-application-type
to none. Now let’s create our JavaFX application class. We’ll call it ChartApplication, since later
in this tutorial this application will be displaying a line chart that updates itself
with stock prices in real time. Since this is our JavaFX application, it needs
to extend Application. This is not currently on the classpath since
we haven’t declared it in our dependencies. Pressing Alt+Enter on the red Application
text gives us the option to Add Maven Dependency. We want to add openjfx javafx-graphics as
a dependency, here we’re using an early access version of 13. If we go to our pom.xml file, we can see IntelliJ IDEA has added this dependency to the file,
and in the background, downloaded the required jar files. We can now import javafx.Application. Application is an abstract class, so we need
to implement some methods. We can get IntelliJ IDEA to implement these
methods by pressing Alt+Enter, selecting Implement methods and choosing the methods to implement. Now we have a JavaFX application, we need to launch it from the Spring Boot application. Instead of using SpringApplication to run
the application, we’ll use the JavaFX Application class, and call launch with the class that
is our JavaFX class, and the application arguments. The reason we need two separate application classes for our application is because of
the way JavaFX uses Java Modules, it’s beyond the scope of this video to go into the details. If we want to use JavaFX and Spring together
but aren’t going to use Modularity from Java 9, this is one way to get it to work. Let’s go back to our JavaFX application class, ChartApplication. We want to be able to make use of Spring functionality
like having access to the application context. Let’s create a field for application context,
this will be a ConfigurableApplicationContext. We will look at how to create this in a minute. Our start method, which is a standard JavaFX
method, is called with a Stage object when the stage is ready to be used. The Stage is what we will draw on when we
have something to show. We can use the Spring pattern of publishing
events via the application context to signal when this Stage is ready. We’ll give the event the Stage so that anything
listening to that event has access to it. This primaryStage parameter was created by
IntelliJ IDEA, let’s rename it to stage to make it a little shorter and easier to read. Now we need to create our StageReadyEvent. Let’s create it as an inner class in ChartApplication
for simplicity. IntelliJ IDEA creates the constructor that
takes stage as a parameter, we’ll pass this into the super constructor. We can make this inner class static. We don’t want the Event to be private as other
classes will be listening for this event. There are some other useful methods in Application that we can override and make use of. Let’s override init(). This is where we need to initialise our application
context. We’ll use the SpringApplicationBuilder, and
give it our SpringBootApplication class which was StockUiApplication, not this ChartApplication. We call run() to get the application context. Since we have an init method, we should probably have some sort of tear down or cleanup too. Let’s override Application’s stop method. Here we can close our application context,
and exit our JavaFX application. Finally, just a bit of cleanup in this class. IntelliJ IDEA is telling us these Exceptions
are not thrown, so we can just go ahead and remove them. Now we have our SpringBoot application class which launches our JavaFX Application. We need something which is going to listen
to the StageReadyEvent that we created. Let’s create a StageInitializer class which
will set up our JavaFX Stage when it’s ready. This class needs to implement ApplicationListener, listening for our StageReadyEvent. This line is a bit long so let’s import the
StageReadyEvent to make the code a bit shorter. We need to implement the methods on this interface, let’s get IntelliJ IDEA to do this for us. The IDE has created an onApplicationEvent
method for us, which takes a StageReadyEvent. Let’s also annotate this class with @Component
so that Spring can do dependency injection and so on here. Let’s go back to the onApplicationEvent method. What we want is to get the JavaFX Stage from
this event so we can do something with this Stage. The getStage method doesn’t exist yet, so
we can get IntelliJ IDEA to create this for us. We want this to return a javafx.Stage. The superclass actually has a method that
does what we want, getSource. This returns a object, so let’s cast this
to Stage. We actually know the source is a Stage because
when we passed our stage constructor parameter into the super constructor, this became the
source. Back in the StageInitializr class, we can assign the result of this method call to a
stage local variable. This stage is ready for us to set up our user
interface. We can run our StockUIApplication, and see
it successfully start up as a SpringBoot application. It does also launch a Java process which would
show a UI if we had created one. We can see we’ve successfully created a JavaFX application which is launched and managed
with Spring, and allows us to use the convenient features of any Spring application. Thanks for watching!


  • Explained very clearly, thx

  • Why are we still writing things in Java? Kotlin does the same thing with fewer lines of code…

Leave a Comment

Your email address will not be published. Required fields are marked *