spring-boot - 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 文件中间接引用的”。
解决方案
好的,我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);
}}
推荐阅读
- sql - 创建选择表中特定条目的 SQL 查询
- spring - Spring集成(IntegrationFlowContext):动态注册新路径到同一个websocket服务器
- php - Laravel Join query is not working for 4 tables
- docker - 在 Heroku 上部署 Tika-tesseract docker
- python-3.x - 如何执行列出本地 pc 上服务器输出的 govc/pyvmomi 运行命令
- git - 如何在 Azure DevOps 服务器的集合之间移动项目?
- dns - 如何在从 IBM 证书管理器订购证书时创建回调 URL?
- reactjs - 如何配置 webpack 以使用 typescript 显示反应组件
- ruby - 如何修复“sudo bundle install”
- angular - 如何在模态单击时在 mat-table 中默认检查 angular8 中特定行的复选框?