users@jersey.java.net

Re: [Jersey] Testing with init-params

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 19 May 2010 17:48:18 +0200

On May 18, 2010, at 2:27 AM, Edwin Shin wrote:

> Pavel,
>
> Thanks for your response. Your test case works for me as well. When
> I was trimming down the code excerpts to include in my original
> email, I changed what was originally a constructor into a regular
> method call.
>
> But it was in fact in a constructor that I was trying to access the
> init-params from ServletConfig. Am I right to understand that this
> is not possible? I seem to recall having read that the context
> injection was available to constructors, so I didn't think more of
> it at the time.
>

Constructor injection is supported. Can you share the code?

I modified Pavel's test csae and it worked fine doing:

         public TestResource(@Context ServletConfig servletConfig) {
             assert(servletConfig != null);
         }


> I worked around this by using a @PostConstruct method where I
> grabbed the init-params I needed for object construction. Would this
> be the recommended way to get at the init-params for object
> construction?
>

IMHO constructor injection is more preferable if you can use a
constructor.

Paul.