JAX-RS:如何 运行 启动方法(无 servlet)

JAX-RS: How to run a method on start (without servlets)

我有一个 JAX-RS(泽西岛)服务器,用于注册和绑定我的东西。

我想在服务器启动时打印横幅。我想使用 JAX-RS 框架 而不是 Web 服务器的平台(即没有 Jetty、Netty、Thorntail 等挂钩)来执行此操作。

我看到以下内容提到了久经考验的 Servlet 做事方式: Jax rs: How can I run a method automatically everytime my server restarts? ,但这不起作用,因为我不是 运行 服务器中的 servlet 容器,因此永远不会进行生命周期调用。

我想一定有一个 JCA-ish 类型的对象,我可以用 Application/ResourceConfig 注册它,它有这样的生命周期调用,但我什至找不到任何类型的列表您可以实际注册的东西。

不要抱怨(但我会),但我无法确定这是否如此困难,因为当他们将项目转移到 eclipse 时,他们破坏了旧官方文档的所有超链接,或者它只是如此含蓄,比如Spring,它只能通过 github'ing 其他人的代码并实现,'oh, I did not know you could do that'。

泽西有 Event Listeners. You'll want to use the ApplicationEventListener and the ApplicationEvent.Type 你可能想听打印横幅是 INITIALIZATION_FINISHED

public class MyApplicationEventListener
            implements ApplicationEventListener {

    @Override
    public void onEvent(ApplicationEvent event) {
        switch (event.getType()) {
            case INITIALIZATION_FINISHED:
                printBanner();
                break;
        }
    }

    @Override
    public RequestEventListener onRequest(RequestEvent requestEvent) {
        return null;
    }
}