Announcing Gluon Ignite: Easy JavaFX Dependency Injection


IgniteOne thing you should realize by now is that the Gluon team doesn’t sit still! In the past few days we’ve announced the release of Gluon Charm developer preview 4, IntelliJ live templates, and the JavaOne conference application. This is just the start – brace yourselves for a very busy October!

Today we are open sourcing a small library that our engineers built called Gluon Ignite. With this library, developers can use popular dependency injection frameworks in their JavaFX applications, including inside their FXML controllers. Gluon Ignite creates a common abstraction over several popular dependency injection frameworks (currently Guice, Spring, and Dagger, but we plan at add more as the demand becomes obvious). With full support of JSR-330 Gluon Ignite makes using dependency injection in JavaFX applications trivial. Here is a quick example of creating an application using the Guice framework and Gluon Ignite.

public class GuiceApp extends Application implements ExampleApp {

    public static void main(String[] args) {
        launch(args);
    }

    private GuiceContext context = new GuiceContext(this, () -> Arrays.asList(new GuiceModule()));

    @Inject private FXMLLoader fxmlLoader;

    @Override public void start(Stage primaryStage) throws IOException {
        context.init();
        fxmlLoader.setLocation(getViewLocation());
        Parent view = fxmlLoader.load();

        primaryStage.setTitle("Guice Example");
        primaryStage.setScene(new Scene(view));
        primaryStage.show();
    }
}

class GuiceModule extends AbstractModule {
    @Override protected void configure() {
        bind(Service.class).to(Service.class);
    }
}

By using Gluon Ignite, you not only get dependency injection inside your application class, but also within your FXML controllers too. Even though the sample above shows a Guice context, as mentioned Gluon Ignite also supports other DI frameworks. By using a DaggerContext or SpringContext in your application, it can be easily set up to work with with those frameworks instead.

To use Gluon Ignite in your software, simply include it as a dependency in your preferred dependency manager. Shown below are examples of how to include Gluon Ignite and one specific implementation in your dependency chain:


Gluon Ignite with Dagger

Maven:


  com.gluonhq
  ignite-dagger
  1.0.2

    

Gradle:

compile 'com.gluonhq:ignite-dagger:1.0.2'


Gluon Ignite with Guice

Maven:


  com.gluonhq
  ignite-guice
  1.0.2

    

Gradle:

compile 'com.gluonhq:ignite-guice:1.0.2'


Gluon Ignite with Spring

Maven:


  com.gluonhq
  ignite-spring
  1.0.2

    

Gradle:

compile 'com.gluonhq:ignite-spring:1.0.2'


Note that ignite-common is automatically included as a dependency for each module, so it is not necessary to include this as a dependency.