Spring Boot Main和JavaFX

卡法里亚

我有可更改游戏板(2D)的Java应用程序。现在,我想要一个JavaFx GUI来可视化该板。

主要:

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:

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);
    }

}

GUI:

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,这很好,但是对于我的用例则不是。

我真正想要的是一个引导,并且它们共享相同的上下文。如何存档?如果有人可以带领我朝正确的方向前进,我将不胜感激。

振亚

您需要针对以下问题进行修复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);
}

}

因此,对于您的示例来说就足够了。您只需传递之前创建的上下文。

但是起初,我认为您不需要将应用程序作为上下文的组成部分-我不知道如何使用它。第二,我认为您将使用fxmlUI并为此使用FxmlLoader该加载器的术语Controller表示这Controller将初始化此类中的所有组件(就JavaFx而言)。因此,为此Controllers可以进行依赖注入FxmlLoader.setControllerFactory(context::getBean);但这仅适用于此功能Controller,不适用于某些视图或面板

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章