如何在JavaFX中使用Spring ?

时间:2021-11-19 17:02:14

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.

我在我的项目中使用javaFX和Scene Builder,我有很多页面。我想避免复杂性,这就是为什么我想使用Spring框架。

So please can anyone explain to me in details how to configure JavaFX with spring framework?

所以请大家给我详细解释一下如何用spring框架配置JavaFX ?

2 个解决方案

#1


8  

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

我搜索了Spring和JavaFX集成示例,但是没有找到一个好的示例。我举了一个例子。您可以查看这个示例应用程序。https://gitlab.com/sunkur/SpringJavaFXController

I hope it helps.

我希望它有帮助。

#2


3  

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.

有许多方法可以将Spring与JavaFX集成在一起。您将发现的大多数技术都是针对FXML控制器上使用FXMLLoader上的API ControllerFactory的bean Spring注入。一种更高级的技术可以将Spring原型bean注入到您的场景中,使用API BuilderFactory(我已经做了测试,它运行得很好),将Spring原型bean注入到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

最后,你谈到了SceneBuilder。SceneBuilder中仍然存在一些问题,而Spring则受到一些人的关注。看看另一篇关于类加载器和SceneBuilder的文章:类路径解析,在SceneBuilder中使用分层的自定义JavaFx组件

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.

如果可能的话,SceneBuilder中不需要知道Spring注入。您可以通过使用Spring上下文的延迟实例(在应用程序的“启动”期间)实现这一点:您的自定义Spring FXMLLoader将只在运行时使用,并且您可以在没有启动Spring时使用普通的FXMLLoader。这样,SceneBuilder就不会用它特有的FXMLLoader加载Spring,也不会出现类路径问题。

#1


8  

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

我搜索了Spring和JavaFX集成示例,但是没有找到一个好的示例。我举了一个例子。您可以查看这个示例应用程序。https://gitlab.com/sunkur/SpringJavaFXController

I hope it helps.

我希望它有帮助。

#2


3  

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.

有许多方法可以将Spring与JavaFX集成在一起。您将发现的大多数技术都是针对FXML控制器上使用FXMLLoader上的API ControllerFactory的bean Spring注入。一种更高级的技术可以将Spring原型bean注入到您的场景中,使用API BuilderFactory(我已经做了测试,它运行得很好),将Spring原型bean注入到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

最后,你谈到了SceneBuilder。SceneBuilder中仍然存在一些问题,而Spring则受到一些人的关注。看看另一篇关于类加载器和SceneBuilder的文章:类路径解析,在SceneBuilder中使用分层的自定义JavaFx组件

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.

如果可能的话,SceneBuilder中不需要知道Spring注入。您可以通过使用Spring上下文的延迟实例(在应用程序的“启动”期间)实现这一点:您的自定义Spring FXMLLoader将只在运行时使用,并且您可以在没有启动Spring时使用普通的FXMLLoader。这样,SceneBuilder就不会用它特有的FXMLLoader加载Spring,也不会出现类路径问题。