users@jersey.java.net

Re: [Jersey] How to access init-param values from a Jersey web application

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 20 May 2010 14:22:15 +0200

On May 20, 2010, at 1:57 PM, Martin Giese wrote:

> Hi,
>
> I have a nice litte web app with a main Application class (in fact, it
> extends PackagesResourceConfig) and a few root resources.
>
> Now I have put some configuration information into web.xml in
> <init-param> elements. It seems that I need a ServletContext or
> ServletConfig to get at the initialization parameters. I know I can
> use
> @Context to inject these into the root resource classes, but that
> means
> I get the values when a request is processed. I would like to get
> them
> at initialization, i.e. in the constructor of the Application
> class. Is
> that possible?
>

Yes, it should be possible to do the following:

public MyApp extends Application {
   @Context ServletConfig sc;
}

If you want you application to be independent of servlet or filters
you can inject:

public MyApp extends Application {
   @Context WebConfig wc;
}

You can also do this:

public MyApp extends Application {
   @Context ResourceConfig rc;
}

add access the init params as properties of "rc". That may seem a
little strange at first but Jersey utilizes it's own ResourceConfig
internally that can be injected. The latter is advantageous if you
want to be independent of web-based deployment.

Paul.