我遵循以下抽象类,使用init()
和stop()
方法:
(以stop()
方法发出)
public abstract class AbstractJavaFxApplication extends Application {
private static String[] fxArgs;
protected ConfigurableApplicationContext applicationContext;
@Override
public void init() throws Exception {
applicationContext = SpringApplication.run(getClass(), fxArgs);
applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
}
@Override
public void stop() throws Exception {
System.out.println("STOP");
applicationContext.stop();
super.stop();
}
protected static void launchApp(Class<? extends AbstractJavaFxApplication> clazz, String[] args){
fxArgs = args;
Application.launch(clazz, args);
}
}
主要类,扩展了AbstractJavaFxApplication
:
@SpringBootApplication
public class WeightliftingviewerApplication extends AbstractJavaFxApplication{
@Value("First attempt")
private String tittle;
@Qualifier("mainView")
@Autowired
private ControllersConfiguration.ViewHolder view;
public static void main(String[] args) {
launchApp(WeightliftingviewerApplication.class, args);
// SpringApplication.run(WeightliftingviewerApplication.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle(tittle);
primaryStage.setScene(new Scene(view.getView()));
primaryStage.setResizable(true);
primaryStage.centerOnScreen();
primaryStage.show();
}
}
问题在于,当我关闭应用程序时,我预期会打入stop()
方法并在控制台中得到STOP
。但是应用程序尚未停止并且仍在运行中
答案 0 :(得分:1)
在您的Stop方法中尝试以下方法:
@Override
public void stop() throws Exception {
System.out.println("STOP");
Platform.exit();
System.exit(0);
}