*** I have a working [demo project](https://github.com/ehayik/jfx-playground/tree/java8) for integrating Spring Boot and JavaFX 8. It requires to download and install Oracle JDK 8 (which includes all JavaFX modules). ![[JavaFX - State of applications development#^619620]] As a result, I had to do a further research to provide the right configuration for a _Maven_/_Gradle_ project to use the latest _JavaFX_ and _Spring Boot_ versions. ### Non-Modular Maven configuration [Maven configuration](https://github.com/ehayik/jfx-playground/blob/master/pom.xml) is pretty straightforward. Use [start.spring.io](https://start.spring.io/) to generate the project. For instance: - ![[spring-io-jdk-17-maven.png]] Then, Add the *JavaFX* modules you need as maven dependencies. For instance: ```xml <properties> <java.version>21</java.version> <javafx.version>21.0.1</javafx.version> </properties> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>${javafx.version}</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>${javafx.version}</version> </dependency> ``` The project can be built and run using [Spring Boot Maven Plugin](https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/maven-plugin/reference/html/) > [!NOTE] > There is a [maven plugin](https://docs.spring.io/spring-boot/docs/2.3.0.RELEASE/maven-plugin/reference/html/) available for running JavaFX 11+ applications. So far, it is not required since the application will be packed and run using Spring Boot. ### Non-Modular Gradle Configuration The [Gradle configuration](https://github.com/ehayik/jfx-playground/blob/master/build.gradle) is a little be trickier, adding the JavaFX modules as dependencies doesn't work 😔. The [JavaFX gradle plugin](https://github.com/openjfx/javafx-gradle-plugin) must be [applied](https://openjfx.io/openjfx-docs/#gradle) in order to load JavaFX modules, as describe below. Apply the plugin: ```groovy plugins { id 'application' id 'org.openjfx.javafxplugin' version '0.1.0' } ``` Next, add the required modules. For instance, if we only need the `javafx.controls` module, we will include: ```groovy javafx { modules = [ 'javafx.controls' ] } ``` > [!NOTE]- Transitive dependencies are automatically resolved > Note that transitive dependencies are automatically resolved (for instance, there is no need to add a dependency for the javafx.graphics module, since it is [transitively](https://openjfx.io/javadoc/15/javafx.controls/module-summary.html) resolved by the javafx.controls module). But if your application is using FXML, you will need to add the javafx.fxml module as well. You can specify a distinct version of JavaFX. For example, if you want to stick to JavaFX 11.0.2: ```groovy javafx { version = '21.0.1' modules = [ 'javafx.controls' ] } ``` > [!NOTE] > So far, the JavaFX plugin is only required to load the JavaFX modules and [Spring Boot Gradle Plugin](https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/) for packaging and running. ### What about modular applications? Spring 5 and Spring Boot are not friendly with [Java Platform Module System (JPMS)](https://www.oracle.com/corporate/features/understanding-java-9-modules.html). Spring won't really be built for modules until [Spring 6/Springboot 3](https://www.infoq.com/news/2021/09/spring-6-spring-boot-3-overhaul/) is released. >Spring Framework 6 is indeed expected to introduce module-info descriptors for all core framework modules, with minimal required dependencies on core Java modules, enabling the JDK's jlink tool to build custom runtime images for Spring setups. There might be constraints with optional third-party libraries and certain configuration strategies, so it is still unclear how popular such explicit module usage will become among Spring users. I do try the modular approach with Spring Boot 2. However, compilation failed due to a known [Lombok error](https://leejjon.medium.com/youll-have-this-problems-when-you-add-lombok-to-a-modular-java-11-micro-service-832f55911bc5). ### How to bootstrap JavaFX application within Spring Boot application? The bootstrap process is heavily inspired by Mr. Awesome Josh Long’s [Spring Tips: JavaFX](https://spring.io/blog/2019/01/16/spring-tips-javafx) installment. Instead of calling `SpringBootApplication.run()` use a [custom bootstrap class](https://github.com/ehayik/jfx-playground/blob/master/src/main/java/com/github/eljaiek/playground/jfx/JavaFxApplication.java) inheriting from JavaFX `Application`. This is needed to initialize *JavaFX* correctly. ```java import javafx.application.Application; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; @SpringBootApplication @ConfigurationPropertiesScan public class MainApplication { public static void main(String[] args) { Application.launch(JavaFxApplication.class, args); } } ``` [JavaFxApplication](https://github.com/ehayik/jfx-playground/blob/master/src/main/java/com/github/eljaiek/playground/jfx/JavaFxApplication.java) class does the heavy lifting for creating a proper JavaFX application with initialized [[What Spring Context is]]. It's responsible for: - Set Spring Boot web server type to `NONE`. - Programmatically create a Spring Boot context in the `Application#init()` method. - Kick off application logic by sending a [`StageReadyEvent`](https://github.com/ehayik/jfx-playground/blob/master/src/main/java/com/github/eljaiek/playground/jfx/JavaFxApplication.java) containing the primary [[JavaFX - Stage | Stage]] as payload. - Support graceful shutdown for both *Spring* context and *JavaFX* platform. *** **References**: - [Spring Tips: JavaFX](https://www.youtube.com/watch?v=lPy9mc_O_gU&t=1821s) - [Creating a Spring Boot JavaFX Application with FxWeaver](https://rgielen.net/posts/2019/creating-a-spring-boot-javafx-application-with-fxweaver/) - [Spring Boot in javaFX application (modularity problems)](https://stackoverflow.com/questions/71536016/spring-boot-in-javafx-application-modularity-problems)