Tutorial: Reactive Spring Boot, Part 3: A JavaFX Spring Boot Application
Learn more about building reactive Spring Boot applications with JavaFX.
Join the DZone community and get the full member experience.
Join For FreeThis is the third part of our tutorial showing how to build a Reactive application using Spring Boot, Kotlin, Java, and JavaFX. The original inspiration was a 70-minute live demo.
This third demonstration shows how to create a JavaFX application that is launched and managed via Spring Boot so that we can use Spring features like Inversion of Control in our JavaFX application.
This blog post contains a video showing the process step-by-step and a textual walk-through (adapted from the transcript of the video) for those who prefer a written format.
This tutorial is a series of steps during which we will build a full Spring Boot application featuring a Kotlin back-end, a Java client, and a JavaFX user interface.
This step shows how to create a Spring Boot JavaFX application so that JavaFX can take advantage of Spring features like Dependency Injection.
Setting Up the Module
In the video, we re-use the client project we created for the previous step and add a new module to it. But if we wanted to create this as a standalone project, and we could create this as a new project rather than a new module, the steps would be very similar (replacing "new module" with "new project").
- With the stock-client project from the previous step open in IntelliJ IDEA, create a new module.
- This is a Spring Boot application, so choose Spring Initializr from the options on the left.
- We're using Java 13 as the SDK for this tutorial, although we're not using any of the Java 13 features (you can download JDK 13.0.1 here, then define a new IntelliJ IDEA SDK for it).
- Enter the group name for the project and call the artifact
stock-ui
. - Keep the defaults of a Maven Project with Java and Jar packaging.
- We'll select Java 11 as the Java version as this is the most recent Long Term Support version for Java, but for the purposes of this project, it makes no difference.
- 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 structure if we wish.
- We don't need to select any Spring Boot Starters for this module.
- 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 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.
The Spring Boot Application Class
As usual, Spring Boot generated a default application class for us. We will need to change this in order to launch a JavaFX application, but for now, we'll just leave this as it is.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StockUiApplication {
public static void main(String[] args) {
SpringApplication.run(StockUiApplication.class, args);
}
}
Updating the Spring Boot Settings
Since this is a JavaFX application and not a web application, add this to the application.properties file of this module:
spring.main.web-application-type=none
Create a JavaFX Application Class
- Create a new Java class in the same package as the Spring application class and call it
ChartApplication
. - (Tip: You can use Alt+Insert for Windows/Linux (⌘N on macOS) in the project window to create a new file or directory).
- Have it extend javafx.application.Application.
This is not currently on the classpath since we haven't added JavaFX to our dependencies yet, so we need to add it to our pom.xml file.
- (Tip: Pressing Alt+Enter on the red Application text in the editor gives the option to "Add Maven Dependency".)
- Add org.openjfx:javafx-graphics as a dependency, version 13.
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>13</version>
</dependency>
- Now import javafx.application.Application in
ChartApplication
. - Application is an abstract class, so we need to override a method.
- (Tip: We can get IntelliJ IDEA to implement these methods by pressing Alt+Enter on the red error, selecting Implement methods, and choosing the methods to implement.)
- We only have one method we need to implement, start.
import javafx.application.Application;
import javafx.stage.Stage;
public class ChartApplication extends Application {
@Override
public void start(Stage stage) {
}
}
Setting Up the Spring Boot Application Class
Now that 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, ChartApplication
, and the application arguments.
import javafx.application.Application;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StockUiApplication {
public static void main(String[] args) {
Application.launch(ChartApplication.class, args);
}
}
The reason we need two separate application classes for our application is because of JavaFX and Java Modules, it's beyond the scope of this tutorial to go into the details. If we want to use JavaFX and Spring together but aren't going to use Java Modules from Java 9, this is one way to get it to work.
Publishing Events via the Application Context
Let's go back to our JavaFX application class, ChartApplication
.
- Create a field
applicationContext
; this will be a ConfigurableApplicationContext. - Our start method, which is a standard JavaFX method, is called with a Stage object when the stage is ready to be used. We can use the Spring pattern of publishing events via the application context to signal when this Stage is ready. Inside
start()
, call applicationContext.publishEvent() with a newStageReadyEvent
. - Pass the stage into the event constructor.
public class ChartApplication extends Application {
private ConfigurableApplicationContext applicationContext;
@Override
public void start(Stage stage) {
applicationContext.publishEvent(new StageReadyEvent(stage));
}
}
Now, we need to create our StageReadyEvent
.
static class StageReadyEvent extends ApplicationEvent {
public StageReadyEvent(Stage stage) {
super(stage);
}
}
- Create it as an inner class in
ChartApplication
for simplicity. It can always be refactored out at a later date. - (Tip: Pressing Alt+Enter on the red
StageReadyEvent
offers the option to "Create inner classStageReadyEvent
). - In the
StageReadyEvent
constructor, pass the stage parameter into the super constructor. - Make this inner class static and package visible, other classes will be listening for this event.
Creating the Application Context
There are some other useful methods in Application that we can override and make use of.
- Override the init() method. This is where we need to initialize our application context.
- (Tip: You can use Ctrl+O within a class to select superclass methods to override).
- Create a new SpringApplicationBuilder, and give it our Spring Boot application class, which is
StockUiApplication
. - Callrun() to get the application context and assign it to the
applicationContext
field.
@Override
public void init() {
applicationContext = new SpringApplicationBuilder(StockUiApplication.class).run();
}
Closing the Application Context
Since we have an init()
method, we should probably have some sort of tear down or cleanup too.
- Override the Application's stop method.
- Inside
stop()
, call applicationContext.close(). - Also call Platform.exit() to end the JavaFX program.
@Override
public void stop() {
applicationContext.close();
Platform.exit();
}
Now, we have our Spring Boot application class, which launches our JavaFX Application class, ChartApplication
:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
public class ChartApplication extends Application {
private ConfigurableApplicationContext applicationContext;
@Override
public void init() {
applicationContext = new SpringApplicationBuilder(StockUiApplication.class).run();
}
@Override
public void start(Stage stage) {
applicationContext.publishEvent(new StageReadyEvent(stage));
}
@Override
public void stop() {
applicationContext.close();
Platform.exit();
}
static class StageReadyEvent extends ApplicationEvent {
public StageReadyEvent(Stage stage) {
super(stage);
}
}
}
Listening to Application Events
We need something which is going to listen to the StageReadyEvent
that we created.
- Create a new class,
StageInitializer
. This will set up our JavaFX Stage when it's ready. - This class should be annotated as a Spring@Component.
- This class needs to implement ApplicationListener, listening for our
StageReadyEvent
. - We need to implement the method on this interface, onApplicationEvent.
- (Tip: IntelliJ IDEA can do this for us, press Alt+Enter on the red error and select "Implement methods").
- The
onApplicationEvent
takes aStageReadyEvent
. CallgetStage
on the event and assign the result to a Stage local variable.
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
public class ChartApplication extends Application {
private ConfigurableApplicationContext applicationContext;
@Override
public void init() {
applicationContext = new SpringApplicationBuilder(StockUiApplication.class).run();
}
@Override
public void start(Stage stage) {
applicationContext.publishEvent(new StageReadyEvent(stage));
}
@Override
public void stop() {
applicationContext.close();
Platform.exit();
}
static class StageReadyEvent extends ApplicationEvent {
public StageReadyEvent(Stage stage) {
super(stage);
}
}
}
(Note: This code will not compile yet)
This method doesn't exist, so we need to create it on StageReadyEvent
.
- (Tip: We can get IntelliJ IDEA to create this for us by pressing Alt+Enter on the red
getStage
method name inStageInitializer
and selecting "Create methodgetStage
"). - The superclass has a method that does what we want, getSource. This returns an object, so call it and cast the returned value to a
Stage
.
static class StageReadyEvent extends ApplicationEvent {
public StageReadyEvent(Stage stage) {
super(stage);
}
public Stage getStage() {
return ((Stage) getSource());
}
}
We know the source is a Stage
because when we passed our stage constructor parameter into the super constructor, this became the source.
Final Steps
The Stage
is ready for us to set up our user interface. We can run our StockUIApplication
and see it successfully start-up as a Spring Boot application. It does also launch a Java process that would show a UI if we had created one. For now, we have successfully created a JavaFX application that is launched and managed with Spring and allows us to use the convenient features of any Spring application.
The full code is available on GitHub.
Further Reading
Tutorial: Reactive Spring Boot, Part 2: A REST Client for Reactive Streams
Published at DZone with permission of Trisha Gee, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments