有没有人知道在Gluon Desktop(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
在使用时为空。
答案 0 :(得分:-1)
你可以使用支持Dagger,Guice和Spring依赖注入的Gluon Ignite(http://gluonhq.com/open-source/ignite/)。 Gluon Ignite是一个开源项目,您可以在存储库网站的测试目录中找到一个示例:https://bitbucket.org/gluon-oss/ignite/src/c85197b33852/src/test/java/com/gluonhq/ignite/?at=default