带有java FX的Spring数据jpa

标签 spring spring-boot javafx-8 openjfx spring-boot-jpa

我将 Spring JPA 与 OpenJFX 结合使用。就是这个项目JavaFX-weaver ,只需在 pom 中添加 spring-boot-start-data-jpa。

但是我的 Spring JPA 启动时间是 15-20 秒,直到 spring 初始化后,UI 才会显示。当用户启动应用程序时,每次都会花费很多时间!

作为解决方法,我尝试创建一个没有 Spring 的简单 java fx 应用程序(使用此演示 here),然后在主要方法中从 spring 中的主要方法开始一个按钮(参见下面的示例)。这将启动 spring,但不会加载依赖项和属性。

你知道练习那个案例的好方法吗?欢迎任何帮助。

谢谢

AppBootstrap(Java + OpenJFX)

public class AppBootstrap extends Application {

    @Override
    public void start(Stage primaryStage) {

        Button btn = new Button();

        // start spring jpa main method
        btn.setOnAction(event -> App.main(new String[]{""})); 

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

应用程序(Spring JPA + javafx-weaver)

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        Application.launch(SpringbootJavaFxApplication.class, args);
    }
}

最佳答案

启动 JPA 驱动的应用程序会增加 ApplicationContext 的加载时间。虽然您可以通过不检查或创建数据库方案来加快速度,例如通过设置 hibernate.hbm2ddl.auto=none,这不是最佳选择。

根据设计,主要阶段在 ApplicationContext 加载后显示,因为它应该能够被依赖注入(inject)。

我推荐的最佳做法是在加载 ApplicationContext 时使用启动画面。这有点棘手,因为您有单独的线程,但大致如下所示:

创建启动窗口

public class Splash {

    private static final int SPLASH_WIDTH = 200;
    private static final int SPLASH_HEIGHT = 200;

    private final Parent parent;
    private final Stage stage; 

    public Splash() {
        this.stage = new Stage();
        stage.setWidth(SPLASH_WIDTH);
        stage.setHeight(SPLASH_HEIGHT);
        Label progressText = new Label("Application loading ...");
        VBox splashLayout = new VBox();
        splashLayout.setAlignment(Pos.CENTER);
        splashLayout.getChildren().addAll(progressText);
        progressText.setAlignment(Pos.CENTER);
        splashLayout.setStyle(
                "-fx-padding: 5; " +
                        "-fx-background-color: white; " +
                        "-fx-border-width:5; " +
                        "-fx-border-color: white;"
        );
        splashLayout.setEffect(new DropShadow());
        this.parent = splashLayout;
    }

    public void show() {
        Scene splashScene = new Scene(parent);
        stage.initStyle(StageStyle.UNDECORATED);
        final Rectangle2D bounds = Screen.getPrimary().getBounds();
        stage.setScene(splashScene);
        stage.setX(bounds.getMinX() + bounds.getWidth() / 2 - SPLASH_WIDTH / 2.0);
        stage.setY(bounds.getMinY() + bounds.getHeight() / 2 - SPLASH_HEIGHT / 2.0);
        stage.show();
    }

    public void hide() {
        stage.toFront();
        FadeTransition fadeSplash = new FadeTransition(Duration.seconds(0.3), parent);
        fadeSplash.setFromValue(1.0);
        fadeSplash.setToValue(0.0);
        fadeSplash.setOnFinished(actionEvent -> stage.hide());
        fadeSplash.play();
    }
}

初始化应用

public class SpringbootJavaFxApplication extends Application {

    private ConfigurableApplicationContext context;

    class ApplicationContextLoader extends Task<Void> {

        private final Stage primaryStage;

        ApplicationContextLoader(Stage primaryStage) {
            this.primaryStage = primaryStage;
        }

        @Override
        protected Void call() {
            ApplicationContextInitializer<GenericApplicationContext> initializer =
                    context -> {
                        context.registerBean(Application.class, () -> SpringbootJavaFxApplication.this);
                        context.registerBean(Stage.class, () -> primaryStage);
                        context.registerBean(Parameters.class,
                                SpringbootJavaFxApplication.this::getParameters); // for demonstration, not really needed
                    };
            SpringbootJavaFxApplication.this.context = new SpringApplicationBuilder()
                    .sources(JavaFxSpringbootDemo.class)
                    .initializers(initializer)
                    .run(getParameters().getRaw().toArray(new String[0]));

            return null;
        }
    }

    @Override
    public void start(Stage primaryStage) {
        var splash = new Splash();
        splash.show();
        final ApplicationContextLoader applicationContextLoader = new ApplicationContextLoader(primaryStage);
        applicationContextLoader.stateProperty().addListener((observableValue, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                context.publishEvent(new StageReadyEvent(primaryStage));
                splash.hide();
            }
        });
        new Thread(applicationContextLoader).start();
    }

    @Override
    public void stop() {
        this.context.close();
        Platform.exit();
    }
}

关于带有java FX的Spring数据jpa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61274423/

相关文章:

java - Spring Boot 存储库双向加入

sql - Spring 启动和 hibernate : update all column's rows where entry is null

JavaFX ListView 从两个列表中选择项目

java - 将 POJO 转换为 JavaFX 属性

java - Spring MVC InternalResourceViewResolver - 无法显示我的页面

java - 无法从Java调用Shell脚本

java - Spring boot AuthenticationSuccessHandler 被忽略

java - 在javafx中更新canvas

java - Log4J2 属性替换 - 使用外部属性文件

java - Camel CXF : compressing response with CXFOutInterceptor throws classcast exception