java - FXML Controller 中的 @Autowired

标签 java spring dependency-injection autowired

我有一个简短的问题。我设法使用 xml 配置文件进行一些依赖项注入(inject),效果非常好! 但是一旦我尝试注入(inject) FXML Controller ,它就不再工作,并且我收到 NullPointerException...

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


<bean id="iMainGUIHandler" class="de.vibrating_earth.main.MainGUIHandler" scope="singleton" autowire="byName"></bean>
<bean id="iMainModel" class="de.vibrating_earth.main.MainModel"  scope="singleton" autowire="byName"></bean>
</beans>

注入(inject)点

@Autowired 私有(private)MainModel iMainModel;

最佳答案

好的,可以关闭,我自己找到了答案:) 我在运行时使用“new”创建了 FXML Controller ,我添加了 FXML Controller 有一个单例 bean,现在它可以工作了!

关于java - FXML Controller 中的 @Autowired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31254786/

相关文章:

java - Java 中的数据访问对象 (DAO)

java - JSP页面只显示文件路径

spring - CSRF token 在登录期间过期

javascript - 将依赖项指定为字符串是否会使 AngularJS 中的 DI 更快?

java - 迭代时如何处理数组中的 null

java - 在 Java 中停止 ArrayList 的迭代

c# - DependencyResolver.Current.GetService 总是返回 null

c# - 如何避免依赖注入(inject)构造函数的疯狂?

Java 动态选项卡

java - ZuulFallbackProvider 没有被调用