Java Spring 4:ClassPathXmlApplicationContext实例化期间的NullPointerException
我正在开发一个独立的Spring4应用程序,它使用javaFX。然而,当我尝试创建一个新的Java Spring 4:ClassPathXmlApplicationContext实例化期间的NullPointerException,java,spring,Java,Spring,我正在开发一个独立的Spring4应用程序,它使用javaFX。然而,当我尝试创建一个新的ClassPathXmlApplicationContext时,所有的自动连接字段都是空的,导致NPE,即使我让Spring实例化我的类 这是我的主要课程: public class Main extends Application { public static void main(String[] args) { launch(); } public void start(Stage sta
ClassPathXmlApplicationContext
时,所有的自动连接字段都是空的,导致NPE,即使我让Spring实例化我的类
这是我的主要课程:
public class Main extends Application {
public static void main(String[] args) {
launch();
}
public void start(Stage stage) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Gui gui = context.getBean(Gui.class);
stage.setScene(new Scene(gui, 400, 400));
stage.show();
}
}
这是我的spring-config.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:annotation-config />
<context:component-scan base-package="org.my.package" />
</beans>
NetManager.class:
@Service
@Scope("singleton")
public class NetManager {
...
}
在创建应用程序上下文期间,NPE甚至在我检索第一个bean之前发生。知道为什么吗?注射顺序如下:
@Autowired
public Gui(NetManager netManager) {
this.netManager = netManager;
netManager.init("Username");
...
}
这就像我还不知道的一个重要细节:)
@Autowired
public Gui(NetManager netManager) {
this.netManager = netManager;
netManager.init("Username");
...
}