Spring Boot 和 JavaFX,使用 WeaverFX

问题描述 投票:0回答:2

我使用 FXML 创建了一个 JavaFX 程序。我被要求将其制作成 Spring Boot 应用程序。我发现这个网站可以帮助我使用 WeaverFX 完成它。 https://www.vojtechruzicka.com/javafx-spring-boot/ 我一步步按照教程进行操作并查看我的代码,一切都一样。但我收到错误。

 @Override
    public void start(Stage stage) {
        FxWeaver fxWeaver = applicationContext.getBean(FxWeaver.class);
        Parent root = fxWeaver.loadView(MyController.class);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
2020-04-08 11:41:16.728  INFO 14512 --- [JavaFX-Launcher] o.s.boot.SpringApplication               : Started application in 1.239 seconds (JVM running for 2.077)
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.myprogram.attempttwo.controller.MyController' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126)
    at net.rgielen.fxweaver.core.FxWeaver.getBean(FxWeaver.java:295)
    at net.rgielen.fxweaver.core.FxWeaver.lambda$load$1(FxWeaver.java:381)
    at java.util.Optional.orElseGet(Optional.java:267)
    at net.rgielen.fxweaver.core.FxWeaver.load(FxWeaver.java:381)
    at net.rgielen.fxweaver.core.FxWeaver.loadView(FxWeaver.java:184)
    at net.rgielen.fxweaver.core.FxWeaver.loadView(FxWeaver.java:125)
    at net.rgielen.fxweaver.core.FxWeaver.loadView(FxWeaver.java:94)
    at com.myprogram.attempttwo.application.JavaFxApplication.start(JavaFxApplication.java:29)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
    ... 1 more

它似乎没有使用“loadView”,但我对 Java 的了解不够,无法弄清楚为什么没有。或者至少要检查一些事情。

***编辑每个请求显示 MyController.class

package com.myprogram.attempttwo.controller;

import com.myprogram.attempttwo.application.WeatherService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import net.rgielen.fxweaver.core.FxmlView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@FxmlView("main-stage.fxml")
public class MyController {

    @FXML
    private Label weatherLabel;
    private WeatherService weatherService;

    @Autowired
    public MyController(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

    public void loadWeatherForecast(ActionEvent actionEvent) {
        this.weatherLabel.setText(weatherService.getWeatherForecast());
    }
}
java spring spring-boot javafx
2个回答
1
投票

我怀疑你的项目结构似乎导致了这个问题。我的意思是你的主类位于

com.myprogram.attempttwo.application
包中,而你的控制器类位于
com.myprogram.attempttwo.controller
包中。

请确保您的 main

JavaFxApplication
位于根包中以初始化其所有 bean。例如,您可以使用
com.myprogram.attempttwo.application.controller
重命名您的控制器包,以便您的控制器位于主类下。


0
投票

补充:这里有两个现成的模板,包含 JavaFX、Spring Boot 和 Maven/Gradle,包括示例代码。

我将感谢 GitHub 上的明星支持它的免费开源,从而帮助您解决问题。 <3

https://github.com/davidweber411/javafx-JavaFxSpringBootGradleApp

https://github.com/davidweber411/javafx-JavaFxSpringBootMavenApp

© www.soinside.com 2019 - 2024. All rights reserved.