春天数据jpa与java FX

问题描述 投票:0回答:1

我正在使用Spring JPA与OpenJFX。就是这个项目 JavaFX-weaver,只需在pom里面添加spring-boot-start-data-jpa即可。

但是我的Spring JPA的启动时间是15-20s,直到Spring启动后UI才会显示。当用户启动应用程序时,每次都要花费大量的时间。

作为一个变通的办法,我试着创建一个没有Spring的简单的java fx应用程序(使用这个演示)。此处),然后在主方法中从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);
    }
}
spring spring-boot javafx-8 openjfx spring-boot-jpa
1个回答
1
投票

启动一个JPA驱动的Application会增加ApplicationContext的加载时间。虽然你可以通过不检查或创建数据库方案来让事情变得更快,例如通过设置 "检查 "或 "创建 "数据库方案。hibernate.hbm2ddl.auto=none但这并不是最好的选择。

在设计上,初级阶段显示为 之后 因为它应该能够被注入依赖关系。

我推荐的最佳实践是在加载ApplicationContext时使用闪屏。这有点棘手,因为你有单独的Threads,但大致上看起来像这样。

创建一个闪屏窗口

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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.