How can i use Guice in JavaFX controllers?

后端 未结 4 1265
眼角桃花
眼角桃花 2021-01-02 20:45

I have a JavaFX application where I would like to introduce Guice because my Code is full of factorys right now, only for the purpose of testing.

I have one use case

相关标签:
4条回答
  • 2021-01-02 20:56

    I don't use Guice, but the simplest approach would appear to be just to use a controller factory on the FXMLLoader. You can create a controller factory that instructs the FXMLLoader to use Guice to initialize your controllers:

    final Injector injector = Guice.createInjector(...);
    FXMLLoader loader = new FXMLLoader(getClass().getResource(...));
    loader.setControllerFactory(new Callback<Class<?>, Object>() {
       @Override
       public Object call(Class<?> type) {
           return injector.getInstance(type);
       }
    });
    // In Java 8, replace the above with 
    // loader.setControllerFactory(injector::getInstance);
    Parent root = loader.<Parent>load();
    
    0 讨论(0)
  • 2021-01-02 20:57

    Assuming you (could) instantiate the controller by hand/guice and not from the FXML, you could use https://github.com/google/guice/wiki/AssistedInject if you need to pass any non DIable parameter to the constructor. You would then set the controller manually to the FXMLLoader with .setController(this) and load the FXML file in the constructor of the controller.

    Not sure if there are any drawbacks, but this kind of system seems to work for me :)

    0 讨论(0)
  • 2021-01-02 21:16

    There's a good DI framework for javaFX called afterburner.fx. Check it out, I think it's the tool you're looking for.

    0 讨论(0)
  • 2021-01-02 21:17

    To use JavaFx with Guice :

    1. Extend javafx.application.Application & call launch method on that class from the main method. This is the application’s entry point.
    2. Instantiate dependency injection container of your choice. E.g. Google Guice or Weld.
    3. In application’s start method, instantiate FXMLLoader and set it’s controller factory to obtain controllers from the container. Ideally obtain the FXMLLoader from the container itself, using a provider. Then give it an .fxml file resource. This will create content of the newly created window.
    4. Give the Parent object instantiated in previous step to Stage object (usually called primaryStage) supplies as an argument to the start(Stage primaryStage) method.
    5. Display the primaryStage by calling it’s show() method.

    Code Example MyApp.java

    public class MyApp extends Application {
        @Override
        public void start(Stage primaryStage) throws IOException {
            Injector injector = Guice.createInjector(new GuiceModule());
            //The FXMLLoader is instantiated the way Google Guice offers - the FXMLLoader instance
            // is built in a separated Provider<FXMLLoader> called FXMLLoaderProvider.
            FXMLLoader fxmlLoader = injector.getInstance(FXMLLoader.class);
    
            try (InputStream fxmlInputStream = ClassLoader.getSystemResourceAsStream("fxml\\login.fxml")) {
                Parent parent = fxmlLoader.load(fxmlInputStream);
                primaryStage.setScene(new Scene(parent));
                primaryStage.setTitle("Access mini Stock App v1.1");
                primaryStage.show();
                primaryStage.setOnCloseRequest(e -> {
                    System.exit(0);
                });
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
    
            launch();
        }
    
    }
    

    Then FXMLLoaderProvider.java

    public class FXMLLoaderProvider implements Provider<FXMLLoader> {
        
            @Inject Injector injector;
        
            @Override
            public FXMLLoader get() {
                FXMLLoader loader = new FXMLLoader();
                loader.setControllerFactory(p -> {
                    return injector.getInstance(p);
                });
                return loader;
            }
        
        }
    

    Thanks to mr. Pavel Pscheidl who provided us with this smart code at Integrating JavaFX 8 with Guice

    0 讨论(0)
提交回复
热议问题