1

有谁知道是否有任何简单的方法可以在 Gluon 桌面(ParticleApplication)应用程序的 FXML 控制器中使用依赖注入?已经有 @Inject 用于类似的事情

public class HomeController {

    @Inject ParticleApplication app;

    @Inject private ViewManager viewManager;

    @Inject private StateManager stateManager;

(作为框架的一部分)但我希望能够@Inject 我自己的对象。


编辑:建议使用 Gluon Ignite 的答案,但我仍然无法弄清楚。这是我尝试的一些代码:

我的 ParticleApplication 类:

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 just in time injection.
        }
    }
}

我的控制器类:

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() {
    }
}

然后我的两个类创建为依赖项:

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 is a test string";

    public TestDependancy() {
    }
}

最后是我的 Gradle 文件:

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'
}

打印出来的"From within GluonApplication Constructor: " + context.getInstance(TestClassToInject.class).testDependancy.testString很好,所以我认为我的依赖类和 Guice 配置好了。但是,System.out.println("From within controller's postInit() method: " + testClassToInject.testDependancy.testString);在控制器内不起作用,因为在testClassToInject使用时为空。

4

1 回答 1

0

您可以使用 Gluon Ignite ( http://gluonhq.com/open-source/ignite/ ),它支持 Dagger、Guice 和 Spring 的依赖注入。Gluon Ignite 是一个开源项目,您可以在存储库网站的测试目录中找到示例:https ://bitbucket.org/gluon-oss/ignite/src/c85197b33852/src/test/java/com/gluonhq /ignite/?at=default

于 2016-04-19T09:08:37.687 回答