JavaFX项目中的Spring Boot @Value注释为null

时间:2019-06-07 09:08:52

标签: java spring spring-boot javafx

我按照本教程进行操作,将Spring Boot和JavaFX https://github.com/mvpjava/springboot-javafx-tutorial集成在一起,当我试图在主类中使用@Value注释时,当JavaFX应用程序启动时,它变为null。

我检查了@PostConstruct和post构造函数中的值注释,它获得了所需的值。我还尝试定义@ConfigurationProperties类,但是在JavaFX应用程序启动时,它也变为null。

这是我的代码:

@SpringBootApplication
public class Main extends Application {

    private ConfigurableApplicationContext springContext;
    private Parent rootNode;

    @Value("${configurations.applicationName}")
    private String applicationName;

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

    @Override
    public void init() throws Exception {
        springContext = SpringApplication.run(Main.class);
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/main.fxml"));
        fxmlLoader.setControllerFactory(springContext::getBean);
        rootNode = fxmlLoader.load();
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(applicationName);
        primaryStage.setScene(new Scene(rootNode));
        primaryStage.show();
    }

    @Override
    public void stop() {
        springContext.close();
    }
}

如前所述,当我进入启动功能时,applicationName为null。

0 个答案:

没有答案