How to use Spring with JavaFX ?

后端 未结 2 1514
暗喜
暗喜 2021-02-05 09:11

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 ex

相关标签:
2条回答
  • 2021-02-05 09:46

    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.

    0 讨论(0)
  • 2021-02-05 10:00

    I searched for Spring and JavaFX integration sample but could not find a good one. So I worked on an example. You can look at this example application. https://gitlab.com/sunkur/SpringJavaFXController

    I hope it helps.

    0 讨论(0)
提交回复
热议问题