users@jersey.java.net

Re: [Jersey] 1.0.3-SNAPSHOT: HttpServletRequest is not injected in constructor?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 13 Feb 2009 15:04:05 +0100

Both issues are now fixed in the trunk,
Paul.

On Feb 13, 2009, at 2:00 PM, Paul Sandoz wrote:

> Hi Alex,
>
> This is an issue, i am currently working on fixing it.
>
> The problem is a registration order issue w.r.t. to the injectable
> providers.
>
> I also found another issue w.r.t. @PostConstruct. Injection occurs
> after, not before, @PostConstruct is called.
>
> Note that if you make any call to the injected HttpServletRequest
> instance in the constructor (or post construct method) then it will
> result in an IllegalStateException. This includes the toString
> method. Thus even the printing out the instance in the constructor
> will fail.
>
> Paul.
>
> On Feb 12, 2009, at 11:04 PM, Roytman, Alex wrote:
>
>> @Context HttpServletRequest httpServletRequest is injected as null
>> in constructor. Howewer if I do field injection it injected proper
>> context
>>
>> --
>>
>> /**
>> * Copyright (c) 2007, Peace Technology, Inc.
>> * $Author: Roytman, Alex$
>> * $Revision: 1$
>> * $Date: 2/11/2009 11:49:26 PM$
>> * $NoKeywords$
>> */
>> package com.peacetech.jaxrs.providers;
>>
>> import com.sun.jersey.core.spi.component.ComponentContext;
>> import com.sun.jersey.core.spi.component.ComponentScope;
>> import com.sun.jersey.spi.inject.Injectable;
>> import com.sun.jersey.spi.inject.InjectableProvider;
>>
>> import javax.jws.WebParam;
>> import javax.servlet.http.HttpServletRequest;
>> import javax.ws.rs.core.Context;
>> import javax.ws.rs.ext.Provider;
>> import java.lang.reflect.Type;
>>
>> @Provider
>> public class TestInjector implements InjectableProvider<WebParam,
>> Type> {
>> private final HttpServletRequest httpServletRequestConstructor;
>> @Context private HttpServletRequest httpServletRequestField;
>>
>> public TestInjector(@Context HttpServletRequest
>> httpServletRequest) {
>> this.httpServletRequestConstructor = httpServletRequest;
>> System.out.println("httpServletRequestConstructor = " +
>> httpServletRequest);
>> }
>>
>> @Override public ComponentScope getScope() {
>> return ComponentScope.PerRequest;
>> }
>>
>> @Override public Injectable<Integer>
>> getInjectable(ComponentContext ic, final WebParam i, Type type) {
>> return new Injectable<Integer>() {
>> @Override public Integer getValue() {
>> System.out.println("httpServletRequestField = " +
>> httpServletRequestField);
>> return 123;
>> }
>> };
>> }
>> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>