JavaFX : Integrating Spring framework with JavaFX app(Incorrect configuration)

爷,独闯天下 提交于 2019-12-01 14:30:33

You have created a SpringFxmlLoader but you are not using it. You want

SpringFxmlLoader loader = new SpringFxmlLoader();
Parent root = (Parent) loader.load(getClass().getResource("login.fxml").toExternalForm());

instead of using the FXMLLoader directly.

I would actually write the SpringFxmlLoader differently, so that it matched the standard FXMLLoader API a little more closely:

public class SpringFxmlLoader {

    private static final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);

    public <T> T load(URL url) {
        try  {
            FXMLLoader loader = new FXMLLoader(url);
            loader.setControllerFactory(applicationContext::getBean);
            return loader.load();
        } catch (IOException ioException) {
            throw new RuntimeException(ioException);
        }
    }
}

Then your start method looks like:

SpringFxmlLoader loader = new SpringFxmlLoader();
Parent root = loader.load(getClass().getResource("login.fxml"));

You might need to tinker with the exact path to get things right, depending on your setup.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!