I am using javaFX with Scene Builder in my project and I have many pages. I want to avoid complexity, that's why I want to use Spring framework.
So please can anyone explain to me in details how to configure JavaFX with spring framework?
I am using javaFX with Scene Builder in my project and I have many pages. I want to avoid complexity, that's why I want to use Spring framework.
So please can anyone explain to me in details how to configure JavaFX with spring framework?
There are many ways to integrate Spring with JavaFX. Most of the techniques you will find aim at Spring injection of beans on FXML controllers using the API ControllerFactory on FXMLLoader. A more advanced technique can inject Spring prototype beans as JavaFX custom components in your Scene with the API BuilderFactory (I have made tests with that it works pretty well) of FXMLLoader.
Finally, you have been talking about SceneBuilder. There are still some issues with SceneBuilder and Spring is concerned by some. Take a look at this other post about classloaders and SceneBuilder: Classpath resolution with hierarchical custom JavaFx components in Scenebuilder
It is necessary for SceneBuilder to not be aware of Spring injection if possible. You can achieve that by using deferred instanciation of Spring Context (during "start" of your Application): your custom Spring FXMLLoader will only be used at runtime, and you can use a vanilla FXMLLoader when Spring is not started. This way SceneBuilder will not load Spring with its vanilla FXMLLoader and you won't have classpath problems.
First, set up your JavaFX class and keep the initialisation in start() only related to jFX. No scenes or objects created here. General stuff.
public class MainStage extends Application {
private static MainStage instance;
private Stage primaryStage;
@Override
public void start(Stage primaryStage) {
/*
* Initialization
*/
instance = this;
this.primaryStage = primaryStage;
}
Now, what Spring does, is basiacally instantiating every class marked as components, during fetching of context. You want to postpone this operation after initialisation of jFX, so jFX beans and components you create will instantiate properly. Running Spring core is as simple as creating new ApplicationContext with one of static implementation methods...
public class MainClass {
private static ApplicationContext context; // nice to have it saved
public static void main(String[] args) {
// jfx init before spring, run async to prevent freezing (for some reason)
CompletableFuture.runAsync(() -> Application.launch(MainStage.class, args));
// spring core init
context = new AnnotationConfigApplicationContext(MainClass.class);
Platform.runLater(() -> {
/*
* Put the follow up here
* set scene to primary stage and show the stage.
*/
// MainStage is not a part of spring context
MainStage.getInstance()
.getPrimaryStage()
.setScene(context.getBean(YourScene.class))
MainStage.getInstance().getPrimaryStage().show();
});
}
© 2022 - 2024 — McMap. All rights reserved.