diff --git a/examples/mini-examples/welcome-example/pom.xml b/examples/mini-examples/welcome-example/pom.xml index 2166b0094..f4ccbd2b0 100644 --- a/examples/mini-examples/welcome-example/pom.xml +++ b/examples/mini-examples/welcome-example/pom.xml @@ -38,6 +38,11 @@ de.saxsys mvvmfx-guice + + com.google.inject + guice + 4.2.2 + org.junit.jupiter @@ -52,7 +57,7 @@ com.google.guava guava - 12.0 + 27.0-jre org.mockito diff --git a/mvvmfx-guice/pom.xml b/mvvmfx-guice/pom.xml index 334f3f304..bac4f417e 100644 --- a/mvvmfx-guice/pom.xml +++ b/mvvmfx-guice/pom.xml @@ -49,13 +49,13 @@ provided - - com.cathive.fx - fx-guice + com.google.inject + guice + [3.0,) + provided - org.assertj diff --git a/mvvmfx-guice/src/main/java/de/saxsys/mvvmfx/guice/MvvmfxGuiceApplication.java b/mvvmfx-guice/src/main/java/de/saxsys/mvvmfx/guice/MvvmfxGuiceApplication.java index 274849a30..b8e69968e 100644 --- a/mvvmfx-guice/src/main/java/de/saxsys/mvvmfx/guice/MvvmfxGuiceApplication.java +++ b/mvvmfx-guice/src/main/java/de/saxsys/mvvmfx/guice/MvvmfxGuiceApplication.java @@ -15,67 +15,65 @@ ******************************************************************************/ package de.saxsys.mvvmfx.guice; -import java.util.List; - -import de.saxsys.mvvmfx.internal.MvvmfxApplication; -import javafx.application.HostServices; -import javafx.stage.Stage; - -import com.cathive.fx.guice.GuiceApplication; import com.google.inject.AbstractModule; -import com.google.inject.Inject; +import com.google.inject.Guice; +import com.google.inject.Injector; import com.google.inject.Module; -import com.google.inject.Provider; - import de.saxsys.mvvmfx.MvvmFX; -import de.saxsys.mvvmfx.guice.internal.GuiceInjector; import de.saxsys.mvvmfx.guice.internal.MvvmfxModule; +import de.saxsys.mvvmfx.internal.MvvmfxApplication; +import javafx.application.Application; +import javafx.application.HostServices; +import javafx.stage.Stage; + +import java.util.ArrayList; +import java.util.List; /** * This class has to be extended by the user to build a javafx application powered by Guice. * * @author manuel.mauky */ -public abstract class MvvmfxGuiceApplication extends GuiceApplication implements MvvmfxApplication { - - - @Inject - private GuiceInjector guiceInjector; +public abstract class MvvmfxGuiceApplication extends Application implements MvvmfxApplication { private Stage primaryStage; - - @Override - public final void init(List modules) throws Exception { + + + /** + * This method is called by the javafx runtime when the application is initialized. + * See {@link Application#init()} for more details. + *

+ * In this method the initialization of the guice container is done. + * For this reason, this method is marked as final and cannot be overwritten by users. + *

+ * Please use {@link #initMvvmfx()} for your own initialization logic. + * + * @throws Exception + */ + public final void init() throws Exception { + List modules = new ArrayList<>(); + modules.add(new MvvmfxModule()); modules.add(new AbstractModule() { @Override protected void configure() { - bind(HostServices.class).toProvider(new Provider() { - @Override - public HostServices get() { - return getHostServices(); - } - }); + bind(HostServices.class).toProvider(MvvmfxGuiceApplication.this::getHostServices); - bind(Stage.class).toProvider(new Provider() { - @Override - public Stage get() { - return primaryStage; - } - }); + bind(Stage.class).toProvider(() -> primaryStage); - bind(Parameters.class).toProvider(new Provider() { - @Override - public Parameters get() { - return getParameters(); - } - }); + bind(Parameters.class).toProvider(MvvmfxGuiceApplication.this::getParameters); } }); this.initGuiceModules(modules); + + final Injector injector = Guice.createInjector(modules); + MvvmFX.setCustomDependencyInjector(injector::getInstance); + + injector.injectMembers(this); + this.initMvvmfx(); } @@ -86,8 +84,7 @@ public Parameters get() { */ public final void start(Stage stage) throws Exception { this.primaryStage = stage; - MvvmFX.setCustomDependencyInjector(guiceInjector); - + this.startMvvmfx(stage); } @@ -97,7 +94,8 @@ public final void stop() throws Exception { } /** - * Configure the guice modules. + * Configure your guice modules. Use the given list and + * add your modules to it. * * @param modules * module list diff --git a/mvvmfx-guice/src/main/java/de/saxsys/mvvmfx/guice/internal/MvvmfxModule.java b/mvvmfx-guice/src/main/java/de/saxsys/mvvmfx/guice/internal/MvvmfxModule.java index 2c28bd3c1..456cd776d 100644 --- a/mvvmfx-guice/src/main/java/de/saxsys/mvvmfx/guice/internal/MvvmfxModule.java +++ b/mvvmfx-guice/src/main/java/de/saxsys/mvvmfx/guice/internal/MvvmfxModule.java @@ -16,8 +16,6 @@ package de.saxsys.mvvmfx.guice.internal; import com.google.inject.AbstractModule; -import com.google.inject.Provider; - import de.saxsys.mvvmfx.MvvmFX; import de.saxsys.mvvmfx.utils.notifications.NotificationCenter; @@ -29,13 +27,6 @@ public class MvvmfxModule extends AbstractModule { @Override protected void configure() { - bind(NotificationCenter.class).toProvider(new Provider() { - @Override - public NotificationCenter get() { - return MvvmFX.getNotificationCenter(); - } - }); - - + bind(NotificationCenter.class).toProvider(MvvmFX::getNotificationCenter); } }