【问题标题】:Could not resolve placeholder for JavaFX application with Spring Boot无法使用 Spring Boot 解析 JavaFX 应用程序的占位符
【发布时间】:2020-12-24 07:26:23
【问题描述】:

我在使用带有 JavaFX 的 Spring Boot 时遇到问题。启动新应用程序时出现以下错误:

原因:java.lang.IllegalArgumentException:无法解析值“${spring.application.ui.title}”中的占位符“spring.application.ui.title”

我按照这个教程:https://blog.jetbrains.com/idea/2019/11/tutorial-reactive-spring-boot-a-javafx-spring-boot-application/ 来创建它。

这是我的课程:

@SpringBootApplication
public class AgricultureApplication {

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

}


public class ApplicationStartUp extends Application {

    private ConfigurableApplicationContext applicationContext;

    @Override
    public void init() {
        applicationContext = new SpringApplicationBuilder(AgricultureApplication.class).run();
    }

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

    @Override
    public void start(Stage primaryStage){
        applicationContext.publishEvent(new StageReadyEvent(primaryStage));
    }

    public static void main(String[] args) {
        launch(args);
    }
}

@Component
public class StageInitializer implements ApplicationListener<StageReadyEvent> {


    private final String applicationTitle;
    private ApplicationContext applicationContext;


    public StageInitializer(@Value("${spring.application.ui.title}") String applicationTitle, ApplicationContext applicationContext){
        super();
        this.applicationTitle = applicationTitle;
        this.applicationContext = applicationContext;
    }


    @Override
    public void onApplicationEvent(StageReadyEvent stageReadyEvent) {
        final Stage primaryStage = stageReadyEvent.getStage();
        try {

            FXMLLoader fxmlLoader = new FXMLLoader(new ClassPathResource("/main.fxml").getURL());
            fxmlLoader.setControllerFactory(aClass -> applicationContext.getBean(aClass));
            Parent parent = fxmlLoader.load();

            Scene scene = new Scene(parent);
            primaryStage.setResizable(false);
            primaryStage.setScene(scene);
            primaryStage.setTitle(applicationTitle);
            primaryStage.show();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

    }
}

public class StageReadyEvent extends ApplicationEvent {

public StageReadyEvent(Stage stage) {
    super(stage);
}

public Stage getStage() {
    return (Stage) this.getSource();
}

}

这是 application.properties 文件:

spring.datasource.url=jdbc:h2:file:./AgricultureRentDb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.main.web-application-type=none
spring.application.ui.title = "Test"

spring.h2.console.enabled=true

还有我的POM.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <groupId>gtech</groupId>
    <artifactId>agriculturerent</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.20.Final</version>
        </dependency>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.8</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>29.0-jre</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>4.3.1</version>
        </dependency>

        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>4.3.1</version>
        </dependency>


        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>16-ea+1</version>

        </dependency>


    </dependencies>

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

            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

</project>

这是我的项目结构:

我认为 spring 不识别 application.properties 文件,我不知道为什么,我尝试添加 @PropertySource 注释并将绝对路径放入 application.properties 文件或将其作为类路径。

请注意,当我按照教程进行操作时,我使用的是 java 8 而不是 java 11,但我认为这不是问题所在。

已编辑: 这是我的项目的 github 链接:https://github.com/Dilyan-Galabov/Agriculture

你能帮我提供问题吗,提前谢谢你:)!

【问题讨论】:

  • 再次检查 application.properties 是否已部署到构建文件夹(默认为 target/classes)。
  • 非常有趣的@James_D 我在那里找不到它嗯..你确定“application.properties”应该在那里吗?
  • 这就是问题所在(或者至少是问题的最直接原因,可能还有其他问题)。由于某种原因,您的构建未部署 application.properties 文件。现在已删除的答案中提到了这一点,但这肯定是在正确的轨道上。
  • 但是我删除了 pom 中不必要的资源,两种方式都没有发生任何事情(留在 pom 中并被删除)。
  • FXML 文件是否部署到target/classes?如果包含 resources 并修复 &lt;directory&gt; 条目会怎样?\

标签: spring spring-boot javafx java-8 application.properties


【解决方案1】:

首先@James_D 发现将资源文件复制到目标文件夹时出现问题。实际上,即使我将其添加到我的 pom.xml 中,它们也不会被复制:

<resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.outputDirectory}</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
</resources>

即使我尝试设置不同的 targetPath 也会出现问题(不会将文件复制到 targetPath),实际上我认为 maven 没有读取 pom 中的代码,我不知道为什么会这样。

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 2018-02-20
    • 1970-01-01
    • 2017-11-16
    • 2018-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2016-05-26
    相关资源
    最近更新 更多