Introducing FxWeaver - Dependency Injection Support for JavaFX and FXML

Fabric

Update 2019-10-28

FxWeaver 1.3.0 released with Spring Boot starter, auto-configuration and direct injection support. Blog post updated.

A few years ago I led a client project where I chose JavaFX 8 as a rich client platform. I never regretted this decision - JavaFX is the most compelling desktop GUI platform I had the pleasure to work with since Borland Delphi 1.0 (yes, I am that old).

Soon I wrapped my head around integrating Dependency Injection and AOP into the workflow. I’m used to have this at hand for more than one and a half decade now - having worked with Spring since version 1, the seeding code of Guice (still to be found in Apache Struts 2 framework), early and late CDI standard and so on. I strongly believe that any Java (the language) project beyond Hello World should build on a solid IoC framework to not end up in a mess.

Besides the usual architectural decoupling and the possibility to effectively apply cross cutting concerns when utilizing Spring and the like, I sought to get more declarative and get rid of boilerplate code once the IoC container was integrated.

The main challenges were

  1. Integrating Spring application context configuration and startup into the JavaFX Application lifecycle

  2. Providing fully managed controllers when using FXML view definitions, without breaking the JavaFX tooling such as SceneBuilder or IDE integration and by using declarative configuration without boilerplate as much as possible

I came up with a suitable solution for challenge 1, which is now superseded by the approach you see later in the example that not only incorporates plain Spring, but full Spring Boot support.

For challenge no. 2 I created a little library called FxWeaver that fitted our needs. It was open source from its inception, yet I did not promote it anyhow since it lacked documentation and what not, and I was somehow reluctant to invest time in supporting it over time.

Lately, however, the topic of integrating Spring or Spring Boot with JavaFX again came to my attention. A new client project hit the road, to be based on JavaFX with Spring Boot and with new engineering staff to implement it. Students of my training courses asked me for pointers on how to integrate JavaFX with Spring Boot. Josh Long issued a Spring Tips installment demonstrating an elegant way to create a JavaFX application with Spring Boot (shamelessly copied in the following example), yet not having FXML in scope. I watched Trisha Gee’s awesome presentation about Spring, Kotlin, and JavaFX Playing Together, finding me thrown back to my own pains when seeing the boilerplate code needed to instantiate a single FXML view with a Spring-managed controller.

That was where I decided to re-visit my little library and give it some love, since others might find it useful as well. Combining JavaFX with Spring Boot is a match made in heaven, so maybe a few other folks come to a point where they find my approach useful.

The main features are:

You can find FxWeaver on Github and on Maven Central. It comes with hopefully useful JavaDocs and sample code that I will dig deeper into in the second part of this article series