Spring Boot和JavaFX,使用WeaverFX

huangapple go评论85阅读模式
英文:

Spring Boot and JavaFX, using WeaverFX

问题

我已经创建了一个带有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
    ...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.myprogram.attempttwo.controller.MyController' available
    ...

看起来好像它没有使用'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());
    }
}
英文:

I've created an JavaFX program with FXML. I was asked to make it into a Spring Boot application. I found this website to help me through it using WeaverFX.
<https://www.vojtechruzicka.com/javafx-spring-boot/>
I followed the tutorial step by step and looking through my code, I have everything the same. Yet I get an error.

 @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 &quot;main&quot; 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 &#39;com.myprogram.attempttwo.controller.MyController&#39; 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

It seems that its not using the 'loadView' but I don't know enough about Java to figure out why it is not. Or at least things to check.

***edit showing MyController.class per request

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(&quot;main-stage.fxml&quot;)
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());
    }
}

答案1

得分: 1

我怀疑你的项目结构似乎引起了这个问题。我的意思是你的主类位于 com.myprogram.attempttwo.application 包中,而你的控制器类位于 com.myprogram.attempttwo.controller 包中。

请确保你的主类 JavaFxApplication 位于根包中,以便初始化所有的 bean。例如,你可以将控制器的包重命名为 com.myprogram.attempttwo.application.controller,这样你的控制器就会在主类下面。

英文:

I suspect that your project structure seems to cause the issue. I mean your main class is in the com.myprogram.attempttwo.application package and your controller classes are in the com.myprogram.attempttwo.controller package.

Please make sure that your main JavaFxApplication is in the root package to get initialized all its beans. For example, you can rename your controller package with com.myprogram.attempttwo.application.controller so that your controllers will be under the main class.

huangapple
  • 本文由 发表于 2020年4月9日 01:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61106887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定