- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 Spring DI 与 JavaFx 一起使用 我有一个 MainController 类,它作为 Bean 加载到 AppConfig 中,然后是另一个将使用 MainService 的 MenuController 类。但是注入(inject)的服务在调用时为空。
应用类:
public class App extends Application {
private static final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class);
@Override
public void start(Stage primaryStage) throws Exception {
MainController mainController = context.getBean(MainController.class);
Scene scene = new Scene(mainController.getView());
primaryStage.setScene(scene);
recursiveWire(context, mainController.getView());
primaryStage.show();
}
public void recursiveWire(AnnotationConfigApplicationContext context, Object root) throws Exception {
context.getAutowireCapableBeanFactory().autowireBean(root);
context.getAutowireCapableBeanFactory().initializeBean(root, null);
for (Field field : root.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(FXML.class) && !Node.class.isAssignableFrom(field.getType())) {
recursiveWire(context, field.get(root));
}
}
}
public static void main(String[] args) {
launch(args);
}
}
AppConfiguration 类:
在这里,我尝试按照我看到的建议设置 ControllerFactory,但它并没有改变结果。
@Configuration
public class AppConfiguration {
@Bean
@Scope("prototype")
public MainService mainService() {
return new InMemoryMainService();
}
@Bean
@Scope("prototype")
@DependsOn("mainService")
public MainController mainController() throws IOException {
return (MainController) loadController("/java/com/akos/fxml/Main.fxml");
}
@Bean
@Scope("prototype")
public MenuController menuController() throws IOException {
return (MenuController) loadController("/java/com/akos/fxml/Menu.fxml");
}
protected Object loadController(String url) throws IOException {
InputStream fxmlStream = null;
try {
fxmlStream = getClass().getResourceAsStream(url);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(url));
Node view = loader.load(fxmlStream);
AbstractController controller = loader.getController();
loader.setControllerFactory(clazz -> controller);
controller.setView(view);
return controller;
} finally {
if (fxmlStream != null) {
fxmlStream.close();
}
}
}
}
菜单 Controller 类:
这是我需要访问 mainService 的地方,但它是空的。
public class MenuController extends AbstractController implements Initializable {
...
@Inject
MainService mainService;
@Override
public void initialize(URL location, ResourceBundle resources) {
disableMenuElements();
mainService.currentProgramProperty().addListener((observable, oldValue, newValue) -> {
...
});
}
}
最佳答案
问题在于事情发生的顺序。
从 Spring 的角度来看,当您请求 bean 时,它是通过调用 menuController()
创建的。应用程序配置中的方法,然后是 @Inject
- 带注释的字段被初始化(通过反射),然后它返回 bean。
然而,menuController()
方法通过加载 fxml 文件创建 Controller ,然后从 FXMLLoader
检索 Controller . initialize()
Controller 中的方法由 FXMLLoader
调用作为 load()
的一部分过程。显然,这发生在你的 menuController()
之前返回(因为它发生在 loader.load()
返回之前);所以initialize()
在 Spring 有机会初始化注入(inject)的字段之前调用。
最快的解决方法可能是为服务定义一个 setter 方法,并在服务初始化时简单地调用服务上的方法:
public class MenuController extends AbstractController implements Initializable {
// ...
private MainService mainService;
@Inject
public void setMainService(MainService mainService) {
this.mainService = mainService ;
mainService.currentProgramProperty().addListener((observable, oldValue, newValue) -> {
// ...
});
}
@Override
public void initialize(URL location, ResourceBundle resources) {
disableMenuElements();
}
}
当我使用 Spring 管理我的 JavaFX 应用程序时,我倾向于使用完全不同的方法。我没有让 Controller 访问 View ,然后从 Controller 检索 View ,而是告诉 FXMLLoader
使用 Spring 实例化 Controller ,通过 controllerFactory
.然后当你调用load()
在 FXMLLoader
上,它从 Spring 请求 Controller 作为 bean,因此 FXMLLoader
接收一个注入(inject)了所有依赖项的 bean。然后当它调用 initialize()
时在 Controller 上,依赖项已经存在。
所以
@Configuration
public class AppConfiguration {
@Bean
// ??? surely a service should be singleton, not prototype, scope...
@Scope("prototype")
public MainService mainService() {
return new InMemoryMainService();
}
@Bean
@Scope("prototype")
public MainController mainController() throws IOException {
return new MainController();
}
@Bean
@Scope("prototype")
public MenuController menuController() throws IOException {
return new MenuController();
}
}
并且您的菜单 Controller 与您拥有的一样:
public class MenuController extends AbstractController implements Initializable {
// ...
@Inject
MainService mainService;
@Override
public void initialize(URL location, ResourceBundle resources) {
disableMenuElements();
mainService.currentProgramProperty().addListener((observable, oldValue, newValue) -> {
// ...
});
}
}
现在你可以做
public class App extends Application {
private static final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class);
@Override
public void start(Stage primaryStage) throws Exception {
// should fix name, you should not start your own pacakge names "java"
FXMLLoader loader = new FXMLLoader(getClass().getResource("/java/com/akos/fxml/Main.fxml"));
loader.setControllerFactory(context::getBean);
Scene scene = new Scene(loader.load());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
请注意,您可以通过调用
在应用程序代码中获取对 Controller 的引用MainController mainController = loader.getController();
在您调用了loader.load()
之后.这为您提供了对 Controller 的引用 FXMLLoader
创建;即 Spring 创建的那个(因为 Controller 工厂指示 FXMLLoader
使用 Spring)。 (不过,在我看来,你真的不需要引用 Controller ; Controller 专门知道如何在 View 和模型(服务)之间进行通信;如果你想从外部更改 UI,你应该更新模型这样做,然后 Controller 将观察模型的变化并更新 View 。)
我不完全清楚“递归连接”应该做什么。如果您通过 <fx:include>
加载菜单在主 fxml 文件中, Controller 工厂将传播到包含的 fxml 文件,因此 MenuController
也将从 spring 上下文实例化,并根据需要注入(inject)服务。如果你在其他地方加载它,你只需要在加载它时设置 Controller 工厂,如上面的主 fxml 文件所示。所有这些都假设您的 Controller 在 fxml 文件中指定为 <fx:controller>
,我认为您的其他代码一定是这种情况。
关于java - Spring-注入(inject)的变量为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36317608/
我正在尝试测试依赖于其他服务 authService 的服务 documentViewer angular .module('someModule') .service('docu
如果我的网站上线(不要认为它会,目前它只是一个学习练习)。 我一直在使用 mysql_real_escape_string();来自 POST、SERVER 和 GET 的数据。另外,我一直在使用 i
我有以下代码,它容易受到 SQL 注入(inject)的攻击(我认为?): $IDquery = mysqli_query($connection, "SELECT `ID` FROM users W
我一直在自学如何创建扩展,以期将它们用于 CSS 注入(inject)(以及最终以 CSS 为载体的 SVG 注入(inject),但那是以后的问题)。 这是我当前的代码: list .json {
这个简单的代码应该通过 Java Spring 实现一个简单的工厂。然而结果是空指针,因为 Human 对象没有被注入(inject)对象(所以它保持空)。 我做错了什么? 谢谢 配置 @Config
我正在编写一个 ASP.NET MVC4 应用程序,它最终会动态构建一个 SQL SELECT 语句,以便稍后存储和执行。动态 SQL 的结构由用户配置以用户友好的方式确定,具有标准复选框、下拉列表和
首先让我说我是我为确保 SQL 注入(inject)攻击失败而采取的措施的知己。所有 SQL 查询值都是通过事件记录准备语句完成的,所有运算符(如果不是硬编码)都是通过数字白名单系统完成的。这意味着如
这是 SQL 映射声称可注入(inject)的负载: user=-5305' UNION ALL SELECT NULL,CONCAT(0x716b6b7071,0x4f5577454f76734
我正在使用 Kotlin 和 Android 架构组件(ViewModel、LiveData)构建一个新的 Android 应用程序的架构,并且我还使用 Koin 作为我的依赖注入(inject)提供
假设 RequestScope 处于 Activity 状态(使用 cdi-unit 的 @InRequestScope) 给定 package at.joma.stackoverflow.cdi;
我有一个搜索表单,可以在不同的提供商中搜索。 我从拥有一个基本 Controller 开始 public SearchController : Controller { protected r
SQLite 注入 如果您的站点允许用户通过网页输入,并将输入内容插入到 SQLite 数据库中,这个时候您就面临着一个被称为 SQL 注入的安全问题。本章节将向您讲解如何防止这种情况的发生,确保脚
我可以从什么 dll 中获得 Intercept 的扩展?我从 http://github.com/danielmarbach/ninject.extensions.interception 添加了
使用 NInject 解析具有多个构造函数的类似乎不起作用。 public class Class1 : IClass { public Class1(int param) {...} public
我有一个 MetaManager 类: @Injectable() export class MetaManager{ constructor(private handlers:Handler
我是 Angular 的新手,我不太清楚依赖注入(inject)是如何工作的。我的问题是我有依赖于服务 B 的服务 A,但是当我将服务 A 注入(inject)我的测试服务 B 时,服务 B 变得未定
我正在为我的项目使用 android 应用程序启动、刀柄和空间。我在尝试排队工作时遇到错误: com.test E/WM-WorkerFactory: Could not instantiate co
我不确定这是什么糖语法,但让我向您展示问题所在。 def factors num (1..num).select {|n| num % n == 0} end def mutual_factors
简单的问题,我已经看过这个了:Managing imports in Scalaz7 ,但我不知道如何最小化注入(inject) right和 left方法到我的对象中以构造 \/ 的实例. 我确实尝
在我的 Aurelia SPA 中,我有一些我想在不同模块中使用的功能。它依赖于调用时给出的参数和单例的参数。有没有办法创建一个导出函数,我可以将我的 Auth 单例注入(inject)其中,而不必在
我是一名优秀的程序员,十分优秀!