首页 > 解决方案 > Spring Boot 和 Java Fx

问题描述

要启动我的应用程序,我避免使用“实现 CommandLineRunner”过程来进行设置,但我在这一行遇到了问题

fxmlLoader.setControllerFactory(springContext::getBean); 

其中 fxmlLoader 是 FxmlLoader 的一个实例,而 springContext 是 ConfigurableApplicationContext 的一个实例。我正面临这个错误,

setControllerFactory(Callback<Class<?>,Object>)FXMLLoader 类型中的方法不适用于参数 (springContext::getBean)”。

谁能帮我准确的语法?我导入的包报告错误为

“无法解析 org.springframework.beans.factory.annotation.Autowire 类型。它是从所需的 .class 文件中间接引用的”。

标签: spring-bootjavafx

解决方案


好的,我ve understood that this would require a little code, but i发现已经使用类似于ve proposed - here示例https://github.com/ruslanys/sample-spring-boot-javafx的解决方案构建了项目

您只是将 javafx 与 spring 联系起来context.getAutowireCapableBeanFactory().autowireBean(this);

在 AbstractJavaFxApplicationSupport.java 文件中

代码看起来像这样

public abstract class AbstractJavaFxApplicationSupport extends Application {

private static String[] savedArgs;

protected ConfigurableApplicationContext context;

@Override
public void init() throws Exception {
    context = SpringApplication.run(getClass(), savedArgs);
    context.getAutowireCapableBeanFactory().autowireBean(this);
}

@Override
public void stop() throws Exception {
    super.stop();
    context.close();
}

protected static void launchApp(Class<? extends AbstractJavaFxApplicationSupport> appClass, String[] args) {
    AbstractJavaFxApplicationSupport.savedArgs = args;
    Application.launch(appClass, args);
}

}

把你看到的所有东西都像豆子一样绑起来

@Configuration

公共类 ControllersConfiguration {

@Bean(name = "mainView")
public ViewHolder getMainView() throws IOException {
    return loadView("fxml/main.fxml");
}

@Bean
public MainController getMainController() throws IOException {
    return (MainController) getMainView().getController();
}

protected ViewHolder loadView(String url) throws IOException {
    InputStream fxmlStream = null;
    try {
        fxmlStream = getClass().getClassLoader().getResourceAsStream(url);
        FXMLLoader loader = new FXMLLoader();
        loader.load(fxmlStream);
        return new ViewHolder(loader.getRoot(), loader.getController());
    } finally {
        if (fxmlStream != null) {
            fxmlStream.close();
        }
    }
}

public class ViewHolder {
    private Parent view;
    private Object controller;

    public ViewHolder(Parent view, Object controller) {
        this.view = view;
        this.controller = controller;
    }

    public Parent getView() {
        return view;
    }

    public void setView(Parent view) {
        this.view = view;
    }

    public Object getController() {
        return controller;
    }

    public void setController(Object controller) {
        this.controller = controller;
    }
}

}

然后在控制器中,您可以一起享受 spring 魔术和 javafx 魔术

public class MainController {
@Autowired private ContactService contactService;

@FXML private TableView<Contact> table;
@FXML private TextField txtName;
@FXML private TextField txtPhone;
@FXML private TextField txtEmail;}

然后像这样启动你的应用程序

@SpringBootApplication
public class Application extends AbstractJavaFxApplicationSupport {

@Value("${ui.title:JavaFX приложение}")//
private String windowTitle;

@Qualifier("mainView")
@Autowired
private ControllersConfiguration.ViewHolder view;

@Override
public void start(Stage stage) throws Exception {
    stage.setTitle(windowTitle);
    stage.setScene(new Scene(view.getView()));
    stage.setResizable(true);
    stage.centerOnScreen();
    stage.show();
}

public static void main(String[] args) {
    launchApp(Application.class, args);
}}

推荐阅读