gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:18 25 4
gpt4 key购买 nike

我正在使用带有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com