如何使用spring依赖注入连接多个fxml控制器? [英] How to wire multiple fxml controllers using spring dependency-injection?

查看:241
本文介绍了如何使用spring依赖注入连接多个fxml控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些基本示例的春季教程,我对如何正确连接事物感到困惑。

I read few spring tutorials with basic examples, and I am a bit confused on how to wire up things properly.

麻烦的是,我想使用应用程序拉取单例控制器引用的上下文,但我读了一些其他主题,除非绝对必要,否则不应直接访问应用程序上下文。我想我应该使用构造函数来实例化我想要的引用,但是这里的事情对我来说都很模糊。

The trouble is, I wanted to use application context to pull singleton controller references, but I read on few other topics that application context should not be accessed directly unless it is absolutely necessary. I think I should use constructor to instantiate reference I want, but here things get all blurry for me.

我有几个fxml文件的javafx应用程序。我有一个主要的fxml,其他的是在main中动态加载的。

I have javafx application with several fxml files. I have one main fxml and other are loaded dynamically inside main.

我将使用简化代码,例如两个fxml控制器, MainController.java (对于主要的fxml)和 ContentController.java (对于内容fxml)

I'll use simplified code for example with two fxml controllers, MainController.java (for main fxml) and ContentController.java (for content fxml)

这个想法是内容fxml有TabPane,而主要的fxml有在ContentController上的TabPane中打开新选项卡的按钮。

The idea is that content fxml has TabPane, and main fxml has button which opens new tab in TabPane on ContentController.

我目前正在做类似这样的事情

I am currently doing something like this

bean xml:

bean xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="contentController"
class="ContentController"
scope="singleton" />
</beans>

MainControler:

MainControler:

public class MainOverlayControler {

ApplicationContext context;

@FXML
private BorderPane borderPane;

@FXML
private void initialize() {
    loadContentHolder();
}
@FXML
private Button btn;

@FXML
private void btnOnAction(ActionEvent evt) {
    ((ContentController)context.getBean("contentController")).openNewContent();
}

private void loadContentHolder() {
    //set app context
    context = new ClassPathXmlApplicationContext("Beans.xml");

    Node fxmlNode;
    FXMLLoader fxmlLoader = new FXMLLoader();
    fxmlLoader.setController(context.getBean("contentController"));

    try {
    fxmlNode = (Node)fxmlLoader.load(getClass().getResource("Content.fxml").openStream());
    borderPane.setCenter(fxmlNode);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

ContentController:

ContentController:

public class ContentController {
@FXML
private TabPane tabPane;

public void openNewContent() {
    Tab newContentTab = new Tab();
    newContentTab.setText("NewTab");
    tabPane.getTabs().add(newContentTab);
    }
}

主要类别:

public class MainFX extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        FXMLLoader fxmlLoader = new FXMLLoader();
        Parent root = (Parent) fxmlLoader.load(getClass().getResource("main.fxml").openStream());
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
    e.printStackTrace();
    }
public static void main(String[] args) {
    launch(args);
    }
}

问题:
我想知道如何用构造函数DI做同样的事情,或者如果我误解了这个概念的其他方式。

Question: I would like to know how to do same thing with constructor DI, or some other way if I misunderstood the concept.

我需要能够调用openNewContent 来自多个其他控制器的ContentController的单例实例。

I need to be able to call "openNewContent" on singleton instance of "ContentController" from multiple other controllers as well.

推荐答案

阅读更多有关JavaFX和Spring集成的内容。我推荐 Stephen Chin 系列

Read some more on JavaFX and Spring integration. I recommend series by Stephen Chin

基本上,您需要将JavaFX类嵌入到Spring上下文生命周期中,并且在链接文章中对它进行了很好的描述。

Basically, You need to embed your JavaFX classes into Spring context life cycle, and it is very nicely described in linked article.

您还可以在 GitHub 上查看他的示例项目,其中使用JavaFX 2.1中引入的控制器工厂简化了代码( 点击此处

You can also check his example project on GitHub, where the code is simplified with controller factories introduced in JavaFX 2.1 (check here)

这篇关于如何使用spring依赖注入连接多个fxml控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆