繁体   English   中英

没有合格的 bean - FxWeaver 和 Spring Boot

[英]No qualifying bean - FxWeaver and Spring Boot

我正在尝试重构我去年编写的 Java 程序以使用 Spring Boot. 前端使用 JavaFX,所以我尝试使用 FxWeaver。

但是,当我运行程序时,启动时出现以下错误:

Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'net.rgielen.fxweaver.core.FxWeaver' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
    ...

rgielen 的网站上看,应该没有必要提供 RxWeaver“......当使用 Spring Boot Starter 时,因为它为 FxWeaver 实例提供了自动配置。”

所以我很困惑为什么我会收到这个错误。 有人可以看看吗?

主要应用

package application;


import org.springframework.boot.autoconfigure.SpringBootApplication;
import javafx.application.Application;


@SpringBootApplication
public class MainApplication  {

    public static void main(String[] args) {

        Application.launch(SpringbootJavaFxApplication.class, args); 
    }

}

SpringBootJavaFxApplication

package application;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import net.rgielen.fxweaver.core.FxWeaver;


public class SpringbootJavaFxApplication extends Application {

    private ConfigurableApplicationContext applicationContext;

    @Override
    public void init() throws Exception {
        this.applicationContext = new SpringApplicationBuilder() 
                .sources(MainApplication.class)
                .run(getParameters().getRaw().toArray(new String[0]));
    }


    @Override
    public void start(Stage stage) {
        FxWeaver fxWeaver = applicationContext.getBean(FxWeaver.class);
        Parent root = fxWeaver.loadView(Controller.class);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }


    @Override
    public void stop() {
        this.applicationContext.close();
        Platform.exit();
    }

}

聚甲醛

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xxxxx</groupId>
    <artifactId>xxxxx</artifactId>
    <version>1.0</version>
    <name>xxxxx</name>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>12.0.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.rgielen</groupId>
            <artifactId>javafx-weaver-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>application.Main</mainClass>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(显然,pom 包含与此问题无关的内容,但我包含了整个文件,以防出现其他任何内容导致此问题。)

如果您已经找出导致问题的原因,请告诉我? 我现在正用这个扯掉我的头发!

另外,如果我没有包含正确/足够的信息,我深表歉意。 这是我在这里的第一篇文章。 我正在使用 eclipse。

谢谢你!

也许我来不及提供帮助,但是:

我根据最小的 pom 和您的 Java 代码重新创建了一个示例。 不过,我无法重现您的问题。

这是我的pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.rgielen.sample</groupId>
    <artifactId>fxweaver-springboot-starter-sample</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>net.rgielen</groupId>
            <artifactId>javafx-weaver-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

你的 pom 有什么让我担心的

  1. 它添加了对 OpenJFX 12(针对 JDK 12)的依赖,但使用目标 1.8 进行编译 - 在 Spring Boot 应用程序中使用java.version属性更容易定义。 当使用带有嵌入式 JavaFX(Oracle、Liberica)的 1.8 JRE 时,您根本不需要 OpenJFX deps
  2. 打包为 Spring Boot 应用程序时不需要javafx-maven-plugin

也就是说,我没有将其视为根本原因。

如果要详细比较,这里的代码: https : //github.com/rgielen/fxweaver-springboot-stripped-demo

只是对将来发现此问题的任何人的简要更新。 我最终无法解决该问题,因此在没有使用 FxWeaver 的情况下对代码进行了全面重构。

更多的劳动密集型,但结果代码可以说是更好的结果。

我有同样的问题。 我查了Spring条件评估报告,发现是这样的:

FxWeaver 自动配置:
不匹配:- @ConditionalOnClass 未找到所需的 class 'javafx.fxml.FXMLLoader' (OnClassCondition)

所以我依赖于javafx-fxml

 <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>12.0.2</version> </dependency>

并且满足了条件。
这对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM