Jersey/ServletContext和启动时加载资源 [英] Jersey / ServletContext and resource loading on startup

查看:192
本文介绍了Jersey/ServletContext和启动时加载资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Java进行Web开发的新人. 我正在开发Web服务,并为此选择了REST/ Jersey .

I'm kind of new in web development with Java. I am developing a web service and I've chosen REST / Jersey for it.

我想在启动服务时初始化一些东西,并保留它们 在服务的整个生命周期中.

I want to init some stuff on startup of the service and to keep them all along the life of the service.

第一个问题: Jersey Servlet的构造函数是这样做的好地方吗?

First question : Is the constructor of the Jersey Servlet a good place to do that ?

基本上,我要做的是加载位于我的WEB-INF目录中的config.ini文件. 遵循此帮助,我了解我需要 ServletContext 来将文件加载为资源.

Basically, what I want to do is to load a config.ini file located in my WEB-INF directory. Following this help, I understand I need a ServletContext to load my file as a resource.

但是,我不清楚如何在 Jersey Servlet中获得此 ServletContext ,因为它实际上不是servlet的实例,而是POJO.带有一些注释. 我想尝试此提示,但是在构造函数中,属性"context"为null.我认为 Jersey 可能会在构造函数之后填充它.是吗?

However, it is not clear to me how to get this ServletContext in a Jersey Servlet, as it is not really an instance of a servlet, but rather a POJO with some annotations. I wanted to try this tip, but the attribute "context" is null in the constructor. I think that Jersey might populate it after the constructor. Right ?

那么正确的方法是什么?

So how is the right way to do this ?

到目前为止,这是我的代码:

Here is my code so far :

/** Main REST servlet */
@Path("/")
public class Servlet {

    // ---------------------------------------------------- 
    // Constants                     
    // ---------------------------------------------------- 

    static private final String CONFIG_PATH = "/WEB-INF/config.ini";

    // ---------------------------------------------------- 
    // Attributes                     
    // ---------------------------------------------------- 

    /** Context */
    @Context ServletContext context;

    // ---------------------------------------------------- 
    // Constructor                     
    // ---------------------------------------------------- 

    /** Init the servlet */
    public Servlet() {

        // Load config.ini from WEB-INF
        Config.config = new Config(
                this.context.getResourceAsStream(CONFIG_PATH));

        // FAIL! this.context is null ...

    }

    // ---------------------------------------------------- 
    // URI Handlers                    
    // ---------------------------------------------------- 

    /** Welcome page */
    @GET
    @Path("/")
    @Produces(MediaType.TEXT_HTML)
    public String welcome() {
        return "<h1>Hi there.</h1>";
    }
}

任何帮助将不胜感激. 预先感谢,

Any help would be much appreciated. Thanks in advance,

拉斐尔

推荐答案

我不熟悉Jersey,但是通常在Java Web应用程序中,我认为正确的做法是创建ContextListener.

I am not familiar with Jersey, but generally in a Java web application, I think the right thing to do would be to create a ContextListener.

上下文侦听器是实现接口javax.servlet.ServletContextListener的类,并在您的web.xml中进行了配置.它具有一种方法,该方法在将应用程序首次放入容器中时执行,而另一种方法在应用程序停止时执行,因此是放置一些一次性初始化内容并清理前内容的理想场所该应用程序已停止.

A context listener is a class that implements the interface javax.servlet.ServletContextListener and is configured in your web.xml. It has a method that is executed when the application is first loded into your container, and another one that is executed when the application is stopped, so it is the ideal place to put some one-time initialization stuff, and clean-up things before the application is stopped.

步骤如下:

  1. 创建您的Listener类,实现contextInitialized(ServletContextEvent sce)方法.在此方法中,您将收到一个带有getServletContext()方法的ServeltContextEvent,该方法使您可以访问ServletContext.
  2. 在web.xml中配置您的监听器

您将在此处找到其他信息:教程

You'll find additional info here : tutorial

或者在Sun的Oracle网站上.

Or on Sun's, er, Oracle's site.

顺便说一句,如果您的文件位于JAR文件中,则我不确定ServletContext方法是否是加载该文件的最佳方法.我认为您最好使用诸如以下的东西:

By the way, if your file will be in a JAR file I am not sure that the ServletContext method is the best way to load it. I think you're better off with somethinbg like :

this.getClass().getClassLoader().getResourceAsStream("com.company.my.file.properties");

这篇关于Jersey/ServletContext和启动时加载资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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