Hi all,
after reading Richard Bair's blog entry on fxexperience.com (
http://fxexperience.com/2011/10/fxml-guice/) I started to play around with
JavaFX2, FXML and - of course - Google Guice. Since I find it annoying to
write the necessary boilerplate stuff over and over again when integrating
Guice into my JavaFX2/FXML based applications I started a little project to
deal with that.
I'm not really sure whether it might be useful for others as well, so I
simply decided to just publish the sources under the Apache License 2.0 on
Github.
Usage of the library is rather simple:
----<EXAMPLE CODE BEGIN>----
package my.guice.examples;
// Other imports to make the example work
import java.util.ResourceBundle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
// Import from fx-guice project
import com.cathive.fx.guice.GuiceApplication;
import com.cathive.fx.guice.GuiceFXMLLoader;
class MyGuiceApp extends GuiceApplication {
@Inject
private GuiceFXMLLoader fxmlLoader;
@Override
public Injector createInjector() {
return Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
// Perform your bindings here, e.g. bind your
// FXML Controller classes that will be referenced in
// your .fxml files via fx:controller="..."
}
});
}
@Override
public void start(Stage primaryStage) throws Exception {
// Use the GuiceFXMLLoader to load our FXML file. It is noteworthy,
that
// the following operations will happen on the controller class
that has
// been defined using fx:controller="..." in the FXML file:
// (1st) dependency injection will be performed
// (2nd) all fields annotated with @FXML will be set and
// (3rd) the initialize method will be called IF the controller
class
// implements javafx.fxml.Initializable.
Parent rootPane =
fxmlLoader.load(getClass().getResource("/ExamplePane.fxml"),
ResourceBundle.getBundle("ExamplePane"));
// Just continue the construction and display of the scene in the
// well known and usual way...
Scene scene = new Scene(rootPane, 600, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("Guicified v2");
primaryStage.show();
}
}
----<EXAMPLE CODE END>----
The main advantage when using my library is, that you can specify the
controller class in you FXML files in the usual and well-documented way
(fx:controller="..."). The GuiceFXMLLoader will make sure that the
instantiation of the controller class on the other hand will be performed
by Guice.
Because of the way that JavaFX Applications are supposed to be started I
decided to overwrite the init-method and made sure that injection of all
fields annotated with @Inject happens here.
The sources can be downloaded from here: https://github.com/cathive/fx-guice
The project lacks documentation and sample code, but Javadoc and unit tests
have already been uploaded.
I have not yet pushed an official release to the Sonatype Nexus Maven
repository, but I plan to provide updated SNAPSHOT releases soon (current
SNAPSHOT release is really outdated).
If there are any JavaFX developers who share my mission (use Google Guice
for dependency injection): I'm looking forward to hearing from you! :)
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/google-guice/-/ZNX_ScV9eVQJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.