sialcasa / mvvmFX

an Application Framework for implementing the MVVM Pattern with JavaFX
Apache License 2.0
484 stars 105 forks source link

Get Guice injector created by mvvmFX #612

Open BowgartField opened 4 years ago

BowgartField commented 4 years ago

Hello, I created a class called StageBuilder which need the same instance of a class B as which is in the ViewModel of my class. I have two classes: StageBuilder and ExampleView. The role of stageBuilder is to load the stage form the FluentViewLoader and attache ressources and windows title.

Those two classes need the same instance of classes B because StageBuilder save the Stage created in a List contained in the class B, and class B is used in ExampleView too. But for now, ExampleView is instanciate by mvvmFX so @Inject annotation is trigered by internal mvvmFX injector. And my StageBuilder is created by my own injector, so this result on a different instance of class B in StageBuilder and ExampleView. My class B is annoted with @Singleton and load in my Guice Module with .asEagerSingleton();

To load the same B instance in StageBuilder I need to get the injector of mvvmFX.

Thanks.

manuel-mauky commented 3 years ago

You can inject the guice injector in you main app class like this:

class MyApp extends MvvmfxGuiceApplication {
    @Inject
    private Injector injector;

    ...
}