On Apr 21, 2010, at 9:05 PM, James Russo wrote:
>>
>> By class variable do you mean field of a class? then no. You can do
>> this:
>>
>> @Component
>> @Scope("singleton")
>> public class MyBean {
>> @Context HttpServletRequest r; // thread local proxy is injected
>>
>>
>> @GET
>> public ... getIt(@Context HttpServletRequest r /* Independent of
>> class scope*/)
>> { ... }
>> }
>>
>
> I guess I don't follow how the thread local proxy injection works.
> Do you need to access this field via the accessor methods then?
No, it works like normal injection except that the instance or
reference injected is a proxy to the real object (which may not
actually exist when injection occurs).
> Can you point me to a reference explaining how that works?
>
Basically you implement a Java Proxy to the HttpServletRequest
interface whose implementation looks up instance of HttpServletRequest
in a ThreadLocal variable then defers the method call to that
instance. It is a common technique used by many dependency injection
frameworks.
Look at the source for WebComponent:
http://fisheye4.atlassian.com/browse/jersey/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/spi/container/servlet/WebComponent.java?r=HEAD
and look at how the field "requestInvoker" is utilized.
Paul.