[英]Spring Boot - Impossible for JavaFX app to implement SmartLifeCycle?
I am building a JavaFX app and i want it to implement Spring's SmartLifeCycle interface to perform tasks when the main class terminates. 我正在构建一个JavaFX应用程序,我希望它实现Spring的SmartLifeCycle接口,以便在主类终止时执行任务。 A JavaFX main class must extend the Application class which contains a stop() method. JavaFX主类必须扩展包含stop()方法的Application类。 The SmartLifeCycle interface also contains a stop method. SmartLifeCycle界面还包含停止方法。 It looks like these 2 methods refuse to co-exist even if they have different method signatures. 即使这两种方法具有不同的方法签名,它们似乎也拒绝共存。 The JavaFX method extended from the Application class has no parameters and throws an exception while the implemented method from SmartLifeCycle takes a Runnable object as an argument. 从Application类扩展的JavaFX方法没有参数,并且会引发异常,而SmartLifeCycle的已实现方法将Runnable对象作为参数。
Is it possible for both these methods to exist in the same class? 这两种方法是否可能存在于同一类中? Both are required to implement by subclasses therefore the compiler complains no matter what i do. 两者都必须由子类实现,因此无论我做什么,编译器都会抱怨。
Thanks 谢谢
The Application
abstract class has the following method: Application
抽象类具有以下方法:
public void stop() throws Exception {}
And the SmartLifecycle
interface has the following method, inherited from Lifecycle
: SmartLifecycle
接口具有从Lifecycle
继承的以下方法:
void stop();
As you can see, one can throw an Exception
and the other cannot. 如您所见,一个可以引发Exception
,而另一个则不能。 If you want to extend Application
and implement SmartLifecycle
, then you can't have throws Exception
in your overridden stop()
method. 如果要扩展Application
并实现SmartLifecycle
,则不能在覆盖的stop()
方法中throws Exception
。
public class MySpringJavaFxApp extends Application implements SmartLifecycle {
@Override
public void start(Stage primaryStage) throws Exception {
// ...
}
@Override
public void stop() {
// ...
}
// other methods that need to be implemented...
}
But note you have to override stop()
to remove the throws
clause. 但是请注意,您必须重写stop()
才能删除throws
子句。 Otherwise the methods clash ( Application#stop
is not abstract, thus tries to implement Lifecycle#stop
in this situation). 否则,方法会发生冲突( Application#stop
不是抽象的,因此尝试在这种情况下实现Lifecycle#stop
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.