How can I use dependency injection in a Gluon Desktop app? -


does know if there easy way use dependency injection within fxml controllers of gluon desktop (particleapplication) app? there @inject used things

public class homecontroller {      @inject particleapplication app;      @inject private viewmanager viewmanager;      @inject private statemanager statemanager; 

(as part of framework) able @inject own objects.


edit: answer suggested use gluon ignite, i'm still having trouble figuring out. here of attempted code:

my particleapplication class:

package com.gluonapplication;  import com.gluonhq.ignite.guice.guicecontext; import com.gluonhq.particle.application.particleapplication; import com.google.inject.abstractmodule; import javafx.scene.scene;  import java.util.arrays;  public class gluonapplication extends particleapplication {      private guicecontext context = new guicecontext(this, () -> arrays.aslist(new guicemodule()));      public gluonapplication() {         super("gluon desktop application");          context.init();         system.out.println("from within gluonapplication constructor: " +                 context.getinstance(testclasstoinject.class).testdependancy.teststring         );     }      @override     public void postinit(scene scene) {         settitle("gluon desktop application");     }      class guicemodule extends abstractmodule {         @override protected void configure() {             // use in time injection.         }     } } 

my controller class:

package com.gluonapplication.controllers;  import com.gluonapplication.testclasstoinject; import com.gluonhq.particle.application.particleapplication; import com.gluonhq.particle.state.statemanager; import com.gluonhq.particle.view.viewmanager; import javafx.fxml.fxml; import javafx.scene.control.label; import javax.inject.inject;  public class primarycontroller {      @inject particleapplication app;      @inject private viewmanager viewmanager;      @inject private statemanager statemanager;      @inject private testclasstoinject testclasstoinject;      @fxml     private label label;      public void initialize() {     }      public void postinit() {         system.out.println("from within controller's postinit() method: " +                 testclasstoinject.testdependancy.teststring         );     }      public void dispose() {     } } 

and 2 classes created dependancies:

package com.gluonapplication;  import javax.inject.inject;  public class testclasstoinject {     @inject     public testdependancy testdependancy;      public testclasstoinject() {     } }  package com.gluonapplication;  public class testdependancy {     public string teststring = "this test string";      public testdependancy() {     } } 

and gradle file:

apply plugin: 'java' apply plugin: 'application'  repositories {     jcenter()     maven {         url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'     } }  mainclassname = 'com.gluonapplication.gluonapplication'  dependencies {     compile 'com.gluonhq:particle:1.1.0'     compile 'com.gluonhq:ignite-guice:1.0.0' } 

the "from within gluonapplication constructor: " + context.getinstance(testclasstoinject.class).testdependancy.teststring prints out fine, think have dependancy classes , guice configured ok. however, system.out.println("from within controller's postinit() method: " + testclasstoinject.testdependancy.teststring); doesn't work within controller because testclasstoinject null @ time of use.

you can use gluon ignite (http://gluonhq.com/open-source/ignite/) supports dependency injection dagger, guice , spring. gluon ignite open source project, , can find example in test directory on repository website: https://bitbucket.org/gluon-oss/ignite/src/c85197b33852/src/test/java/com/gluonhq/ignite/?at=default


Comments

Post a Comment

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -