I want to combine two libraries - JavaFx and Spring Boot, thus I need to have an instance of ApplicationContext
in my start() method in main class:
Main class:
@SpringBootApplication
public class JavaFXandSpringBootTestApplication extends Application{
@Autowired
ApplicationContext ctx;
public static void main(String[] args) {
SpringApplication.run(JavaFXandSpringBootTestApplication.class, args);
launch();
}
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/LogIn.fxml"));
loader.setController(ctx.getBean(LogInController.class)); // HERE OCCURES ERROR
Scene scene = new Scene((Parent)loader.load());
stage.setScene(scene);
stage.show();
}
}
Controller class:
@Component
public class LogInController {
}
As I have read the documentary @SpringBootApplication
enables auto detect of Components and under the hood is also annotating the class with @Configuration
.
So now, why am i reciving a NullPointerException in commended line? It's because that ctx isn't injected properly, but why? What am i doing wrong?