Contexts and Dependency Injection for JavaFX

A library that allows you to use "Contexts and Dependency Injection" in JavaFX applications.

License

License

Categories

Categories

JavaFX User Interface
GroupId

GroupId

de.perdoctus.fx
ArtifactId

ArtifactId

javafx-cdi-bootstrap
Last Version

Last Version

2.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Contexts and Dependency Injection for JavaFX
A library that allows you to use "Contexts and Dependency Injection" in JavaFX applications.
Project URL

Project URL

https://github.com/cgiesche/javafx-cdi-bootstrap
Project Organization

Project Organization

Christoph Giesche
Source Code Management

Source Code Management

https://github.com/cgiesche/javafx-cdi-bootstrap.git

Download javafx-cdi-bootstrap

How to add to project

<!-- https://jarcasting.com/artifacts/de.perdoctus.fx/javafx-cdi-bootstrap/ -->
<dependency>
    <groupId>de.perdoctus.fx</groupId>
    <artifactId>javafx-cdi-bootstrap</artifactId>
    <version>2.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/de.perdoctus.fx/javafx-cdi-bootstrap/
implementation 'de.perdoctus.fx:javafx-cdi-bootstrap:2.0.0'
// https://jarcasting.com/artifacts/de.perdoctus.fx/javafx-cdi-bootstrap/
implementation ("de.perdoctus.fx:javafx-cdi-bootstrap:2.0.0")
'de.perdoctus.fx:javafx-cdi-bootstrap:jar:2.0.0'
<dependency org="de.perdoctus.fx" name="javafx-cdi-bootstrap" rev="2.0.0">
  <artifact name="javafx-cdi-bootstrap" type="jar" />
</dependency>
@Grapes(
@Grab(group='de.perdoctus.fx', module='javafx-cdi-bootstrap', version='2.0.0')
)
libraryDependencies += "de.perdoctus.fx" % "javafx-cdi-bootstrap" % "2.0.0"
[de.perdoctus.fx/javafx-cdi-bootstrap "2.0.0"]

Dependencies

provided (2)

Group / Artifact Type Version
javax.enterprise : cdi-api jar 2.0.SP1
org.openjfx : javafx-fxml jar 11-ea+25

Project Modules

There are no modules declared in this project.

Build Status Maven Central

Contexts and Dependency Injection for JavaFX

This library allows you to use Contexts and Dependency Injection (CDI 2.0) in your JavaFX application. It is up to you, which CDI2 implementation you want to use.

Getting Started

First make sure, you have added javafx-cdi-bootstrap to your classpath. If you are using maven you can achive this by adding the following dependency to your pom.xml:

<dependency>
    <groupId>de.perdoctus.fx</groupId>
    <artifactId>javafx-cdi-bootstrap</artifactId>
    <version>2.0.0</version>
</dependency>

If not already present, add a CDI 2.0 implementation (Weld implementation for exampe):

<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se-core</artifactId>
    <version>3.0.5.Final</version>
</dependency>

Then add the following beans.xml file to your META-INF directory, to enable automatic bean discovery:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" bean-discovery-mode="all">
</beans>

After that, simply create a class that extends FxWeldApplication. It is very similar to the standard Application class from JavaFX core and can be used equally. It also provides a main-method to start the CDI container and your JavaFX application.

public class Main extends FxCdiApplication {

    @Inject
    @Bundle("bundles/Application")
    private final FXMLLoader fxmlLoader;

    public void start(final Stage stage, final Application.Parameters parameters) throws Exception {
        final Parent mainWindow = fxmlLoader.load(getClass().getResourceAsStream("application.fxml"));
        final Scene scene = new Scene(mainWindow);
        stage.setScene(scene);
        stage.show();
    }

}

As you can see, the FXMLLoader is already being injected by CDI. The @Bundle annotation assigns the provided ResourceBundle to the injected FXMLLoader. An instance of the controller class defined in the FXML file will automatically be created and managed within the CDI context.

<VBox fx:controller="de.perdoctus.fx.Contoller">
   <children>
      <Label text="%label-text" />
      <Button text="%button-text" />
   </children>
</VBox>

From now on, you are fully CDI enabled. You can write your own producers, inject everything into controllers and even use CDI events.

public class Controller {
    
    @Inject
    private XyService xyService;
    
    @Inject
    @Bundle
    private ResourceBundle rb;
    
    @Inject
    private Event<FooBar> foobarEvent;
    
    public void on(@Observes Event<OtherEvent> event) {
        ...
    }
    
    ...
}

Cheat-Sheet

This library includes producers for FXMLLoader and ResourceBundle classes. This is, how you can use them:

Inject FXMLLoader:

@Inject
private FXMLLoader fxmlLoader;

Inject FMXLLoader with "included" ResourceBundle:

@Inject
@Bundle("resourceBundles/General")
private FXMLLoader fxmlLoader;

Inject Instance<> of FXMLLoader to be able to receive multiple fresh instances for loading different FXML files.

@Inject
private Instance<FXMLLoader> fxmlLoader;

public void loadFxmls() throws Exception {
    final FXMLLoader wizardPage1Loader = fxmlLoader.get(); // Fresh instance #1
    final Node wizardPage1View = wizardPage1Loader.load(getClass().getResourceAsStream("/fxml/WizardPage1.fxml"));
    final Object wizardPage1Controller = wizardPage1Loader.getController();

    final FXMLLoader wizardPage2Loader = fxmlLoader.get(); // Fresh instance #2
    final Node wizardPage2View = wizardPage2Loader.load(getClass().getResourceAsStream("/fxml/WizardPage2.fxml"));
    final Object wizardPage2Controller = wizardPage2Loader.getController();
}

Inject a ResourceBundle:

@Inject
@Bundle("resourceBundles/LoginDialog")
private ResourceBundle loginDialogRb;

Versions

Version
2.0.0
1.0.3
1.0.2