美好的一天。我的JavaFX应用程序有一个奇怪的行为。我使用弹簧将控制器注入装载机,但中途变为NULL。我有一个这样的课堂阶段:
@org.springframework.stereotype.Service
public class Stage extends Application {
private Controller controller;
private static final Logger logger = LoggerFactory.getLogger(Stage.class);
@Autowired
@Lazy
public Stage(Controller controller){
logger.debug("constructor");
logger.debug(controller.toString());
this.controller = controller;
}
public Stage() {
}
@Override
public void start(javafx.stage.Stage primaryStage) throws Exception {
logger.debug("start(primarystage)");
FXMLLoader loader = new FXMLLoader();
URL location = getClass().getResource("/company/sample.fxml");
loader.setLocation(location);
logger.debug(loader.toString());
logger.debug(controller.toString());
loader.setController(controller);
Parent parent = loader.load();
Scene scene = new Scene(parent, 300, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public void start(){
logger.debug("start()");
launch();
}
}
这是一个日志:
10:26:42.887 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'controller'
10:26:43.035 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'stage' <--------------
10:26:43.035 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'stage' <-------------------
10:26:43.040 [main] DEBUG company.ui.Stage - constructor <------ constructor have been executed
10:26:43.040 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'controller' <--- spring given him a 'controller' bean
10:26:43.053 [main] DEBUG company.ui.Stage - company.Controller@130161f7 <---- it is not null
10:26:43.054 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'stage' to allow for resolving potential circular references
10:26:43.069 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'stage'
10:26:43.074 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'stage'
10:26:43.074 [main] DEBUG company.ui.Stage - start() <-- start() have been executed
10:26:43.323 [JavaFX Application Thread] DEBUG company.ui.Stage - start(primarystage) <------ start(primary stage) have been executed
Exception in Application start method
10:26:43.332 [JavaFX Application Thread] DEBUG company.ui.Stage - javafx.fxml.FXMLLoader@420ac8a9
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$1/793138072.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException <----- controller is null!!!!!
at company.ui.Stage.start(Stage.java:42)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1910484864.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
我无法理解它是如何发生的。