Spring应用程序上下文为null [英] Spring application context is null

查看:219
本文介绍了Spring应用程序上下文为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Spring Boot 1.3.3, with JavaFX. The application successfully launches Splash screen with this(at this point applicationContext is not null)

@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 bean都为空.

Problem is in AppSplashController applicationContext and primaryStage beans are all null.

@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接口

BootInitializable interface

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

这是我在春季和春季靴子上的第一个项目,请原谅我的无知.

This is my first project in spring and spring boot, so pardon my ignorance.

推荐答案

我认为您需要指定spring来设置这些bean的自动装配.例如

I think you need specify spring to set autowire these beans. For example

@Autowired
private ApplicationContext springContainer;

另一个建议是使用ApplicationContextProvider,它实现了Spring ApplicationContextAware接口.

Another suggestion is use ApplicationContextProvider which implements springs ApplicationContextAware interface.

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();

这篇关于Spring应用程序上下文为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆