Jersey 应用程序在启动时运行初始化代码以初始化应用程序 [英] Jersey app-run initialization code on startup to initialize application

查看:54
本文介绍了Jersey 应用程序在启动时运行初始化代码以初始化应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Jersey 构建的应用程序.我需要通过运行特定于应用程序的登录名/代码在 Tomcat 7 容器中启动 webapp/war 时进行一些初始化.

I have an app built with Jersey.I need to do some initialization on startup of the webapp/war in the Tomcat 7 container by running an application specific login/code.

使用 Jersey 最好的方法是什么?我之前在 Servlet 环境中使用了 ContextListener 和 contextInitialized().在进行此调用之前,我需要确保 Jersey 资源已加载.

What is the best way to do this with Jersey ? I have used ContextListener with contextInitialized()before in a Servlet environment. I need to make sure the Jersey resources are loaded before I make this call.

推荐答案

不知道你说的之前加载了泽西资源"是什么意思,但是如果你想真正插入泽西初始化进程..泽西有几个监控"" 插件点(没有广泛宣传或记录)以及我要描述的内容是在 AbstractResourceModel 初始化之后调用的 - 所以在应用启动之后.

Not sure what you mean by "Jersey resources are loaded before", but if you want to really plug in into Jersey init process.. Jersey has several "monitoring" plugin points (not widely advertised or documented) and what I'm going to describe is being called after initialization of AbstractResourceModel - so right after app startup.

试试这个:

@Provider
public class Listener implements AbstractResourceModelListener {

    @Override
    public void onLoaded(AbstractResourceModelContext modelContext) {
        System.out.println("##### resource model initiated");
    }
}

每个应用程序生命周期应该只发生一次,我不太确定是否重新加载,但如果您不使用该功能,则无需为此烦恼(无论如何,您应该在那里进行检查以避免多次调用 if 可能会导致一些问题).

It should happen only once per app lifecycle, I'm not very sure about reloading, but you don't need to bother by it if you are not using that feature (anyway, you should put some check there to avoid multiple invocation if could cause some issues).

这篇关于Jersey 应用程序在启动时运行初始化代码以初始化应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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