java - Spring 应用程序上下文为空

标签 java spring javafx spring-boot

我正在使用带有 JavaFX 的 Spring Boot 1.3.3。应用程序成功启动闪屏(此时 applicationContext 不为空)

@SpringBootApplication
public class PirconApplication extends Application{

@Bean(name = "primaryStage")
public Stage getPrimaryStage() {
    return new Stage(StageStyle.DECORATED);
}

private ConfigurableApplicationContext applicationContext = null;
private static String[] args = null;

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

@Override
public void start(Stage primaryStage) throws Exception {
    Task<Object> worker = worker();
    worker.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent event) {
            try {
                AppSplashController loader = applicationContext.getBean(AppSplashController.class);
                Stage stage = applicationContext.getBean(Stage.class);
                stage.setScene(loader.init());
                stage.initStyle(StageStyle.UNDECORATED);
                stage.show();
            } catch (IOException e) {
                    e.printStackTrace();
                    System.exit(0);
            }
        }
    });
    worker.setOnFailed(new EventHandler<WorkerStateEvent>() {

            @Override
            public void handle(WorkerStateEvent event) {
                    // TODO Auto-generated method stub
                    System.exit(0);
            }
    });
    worker.run();
}
public static void main(String[] args) {
    PirconApplication.args = args;
    launch(PirconApplication.class, args);
}

private Task<Object> worker() {
    return new Task<Object>() {
        @Override
        protected Object call() throws Exception {
                applicationContext = SpringApplication.run(PirconApplication.class, args);
                return null;
        }
    };
}
}

问题出在 AppSplashController applicationContext 和 primaryStage beans 都是空的。

@Component
public class AppSplashController implements BootInitializable {

@FXML
private ImageView imgLoading;
@FXML
private Text lblWelcome;
@FXML
private Text lblRudy;
@FXML
private VBox vboxBottom;
@FXML
private Label lblClose;

private Stage primaryStage;

private ApplicationContext springContainer;
/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {
    System.out.println("applicationContext: "+springContainer);
    System.out.println("primaryStage: "+primaryStage);
}

}

@Autowired
@Override
public void setPrimaryStage(Stage primaryStage) {
    this.primaryStage = primaryStage;
}

@Override
public Scene init() throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("/com/pircon/views/splash.fxml"));
    return new Scene(root);
}

@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
    this.springContainer = ac;
}

}

BootInitializable接口(interface)

public interface BootInitializable extends Initializable, ApplicationContextAware {
    public Scene init() throws IOException; 
    public void setPrimaryStage(Stage primaryStage);
}

这是我在 spring 和 spring boot 中的第一个项目,请原谅我的无知。

最佳答案

我认为您需要指定 spring 来设置 Autowiring 这些 bean。例如

@Autowired
private ApplicationContext springContainer;

另一个建议是使用实现 spring ApplicationContextAware 接口(interface)的 ApplicationContextProvider。

public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext ctx) {
        context = ctx;
    }
}

您可以通过ApplicationContextProvider.getApplicationContext();获取应用上下文

关于java - Spring 应用程序上下文为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36985189/

相关文章:

java - 如何: Organize Buttons and stuff with JavaFX

ajax - spring 400 错误请求。我该如何修复它,或者至少看看是什么原因造成的?

java - Spring 中的任务调度程序

java - 如何将 propertyvaluefactory 指向 map 的值?

JavaFX ControlsFX 自动完成 : How to get popup result into a new ObservableList

java - 如何在 Spring mvc 中延迟方法的执行几毫秒?

java - 使用 datainputstream 和 bufferedinputstream 接收文件时陷入无限循环

java - Wildfly 9.x 无法编码希腊附件文件名

java - 将 Javascript 中的字符串发送到 Play Framework 中的 java 方法

java - 继承和对象创建