Spring Boot Main 和 JavaFX [英] Spring Boot Main and JavaFX

查看:46
本文介绍了Spring Boot Main 和 JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有改变游戏板 (2D) 的 Java 应用程序.现在我想要一个 JavaFx GUI 来可视化电路板.

I have Java Application that changes a game board (2D). Now I want to have a JavaFx GUI to visualize the board.

主要内容:

package example;

import example.common.MyService;
import example.gui.GUI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan({"example"})
public class Main implements CommandLineRunner {

    @Autowired
    MyService myService;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
        GUI.launchApp(GUI.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("gameloop or something");
        System.out.println(myService.getSomething());
    }

}

AbstractJavaFxApplicationSupport:

AbstractJavaFxApplicationSupport:

package example.gui;


import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public abstract class AbstractJavaFxApplicationSupport extends Application {

    private static String[] savedArgs;
    static ConfigurableApplicationContext applicationContext;

    @Override
    public void init() throws Exception {
        super.init();
        applicationContext = SpringApplication.run(getClass(), savedArgs);
        applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
    }

    @Override
    public void stop() throws Exception {
        super.stop();
        applicationContext.close();
    }

    public static void launchApp(Class<? extends AbstractJavaFxApplicationSupport> appClass, String[] args) {
        AbstractJavaFxApplicationSupport.savedArgs = args;
        Application.launch(appClass, args);
    }

}

界面:

package example.gui;

import example.common.MyService;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GUI extends AbstractJavaFxApplicationSupport {

    @Autowired
    private MyService myService;

    @Override
    public void start(Stage primaryStage) throws Exception {

        if (null == myService) {
            throw new IllegalStateException("Service was not injected properly");
        }

        primaryStage.setTitle("Spring with JavaFX");

        StackPane root = new StackPane();
        root.getChildren().add(new Label("Hello World with " + myService.getSomething()));

        Scene scene = new Scene(root);
        scene.setFill(Color.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

我的服务:

package example.common;

import org.springframework.stereotype.Component;

@Component
public class MyService {
    public int getSomething() {
        return 42;
    }
}

大多数 JavaFx spring boot 集成如上所示:它们规定 GUI 作为应用程序的入口点.如果我运行此示例,则会启动两个单独的应用程序(显然.因为有两个 SpringApplication.run 调用).如果你想要一个独立的 GUI,这很好,但对于我的用例来说不是.

Most JavaFx spring boot integrations are like the shown above: They prescribe the GUI as entry point for the application. If I run this example two individual applications are booted (obviously. Because there are two SpringApplication.run calls). If you want a Standalone GUI this is fine but for my usecase it is not.

我真正想要的是一个引导并且它们共享相同的上下文.如何存档?如果有人能引导我走向正确的方向,我将不胜感激.

What I really want is one boot and that they share the same context. How to archive this? I would be grateful if somebody could lead me in the right direction.

推荐答案

您需要针对您的 AbstractJavaFxApplicationSupport 进行修复:

You need follow fix for your AbstractJavaFxApplicationSupport:

import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

@Component
public abstract class AbstractJavaFxApplicationSupport extends Application {

static ConfigurableApplicationContext applicationContext;

@Override
public void init() throws Exception {
    super.init();
    applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
}

@Override
public void stop() throws Exception {
    super.stop();
    applicationContext.close();
}

public static void launchApp(Class<? extends AbstractJavaFxApplicationSupport> appClass, ConfigurableApplicationContext context, String[] args) {
    applicationContext = context;
    Application.launch(appClass, args);
}

}

所以,对于你的例子,这就足够了.您只需传递之前创建的上下文.

So, for your example thats enough. You just pass context created before.

但一开始,我认为您不需要将您的应用程序作为上下文的组件 - 我不知道您如何使用它.其次,我认为您将在 UI 中使用 fxml,为此您可以使用 FxmlLoader.这个加载器有术语 Controller ,这意味着这个 Controller 将初始化这个类中的所有组件(就 JavaFx 而言).因此,对于此 Controllers 的依赖注入,您可以使用方法 FxmlLoader.setControllerFactory(context::getBean);.但它只适用于这个Controller,不适用于某些视图或面板

But at first, I don't think that you need make your application as component of context - I don't know how can you use it. At second, I think you will use fxml for your UI, and for this purposes you can use FxmlLoader. This loader has term Controller which means that this Controller will initialize all components (in terms of JavaFx) in this class. So, for dependency injection for this Controllers you can use method FxmlLoader.setControllerFactory(context::getBean);. But it will work only for this Controller's, not for some views or panels

这篇关于Spring Boot Main 和 JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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