users@jersey.java.net

Re: [Jersey] injecting UriInfo in constructor

From: Arul Dhesiaseelan <arul_at_fluxcorp.com>
Date: Thu, 4 Feb 2010 10:03:03 -0700

Yea, that works.

Thanks,
-Arul

On Feb 4, 2010, at 9:33 AM, Paul Sandoz wrote:

>
> On Feb 4, 2010, at 5:24 PM, Arul Dhesiaseelan wrote:
>
>> Thanks Paul.
>>
>> Btw, I am not using any of those injection frameworks in this case.
>
>
> OK, then constructor injection should work:
>
> @Singleton
> @Path("/")
> public class ClusterResource {
> public ClusterResource(@Context UriInfo uriInfo) {
> ....
> }
> }
>
> However, since the class is a singleton what will get injected is a reference to a thread local proxy, so if you access any methods of the reference in the constructor, including toString, then you will get an IllegalStateException. You need to access the reference when a request is in scope.
>
> Paul.
>
>
>
>> Do you think the hack would still work in that case?
>>
>> I tried implementing your hack, but the injected component is still null inside the constructor. Do I need to register the component or do somthing? Here is what I tried.
>>
>> public class UriInjectionComponent implements UriInfo {
>> @Context
>> UriInfo uriInfo;
>>
>> public String getPath() {
>> return uriInfo.getPath();
>> }
>> ......
>>
>> }
>>
>> And the resource looks:
>>
>> @Singleton
>> @Path("/")
>> public class ClusterResource {
>> public ClusterResource(@Context UriInjectionComponent uriInfo) {
>> ....
>> }
>> }
>>
>
>
>>
>> -Arul
>>
>>
>> On Feb 4, 2010, at 2:27 AM, Paul Sandoz wrote:
>>
>>> Hi Arul,
>>>
>>> Constructor injection of JAX-RS/Jersey artifacts when using Guice, CDI or Spring is not currently supported. In such cases you need to use field in conjunction with @PostConstruct or use a setter method.
>>>
>>> One hacky way of doing this is to create a component with UriInfo injected into a field, then inject that component into the constructor. That component can implement the UriInfo interface and defer to the injected UriInfo.
>>>
>>> Paul.
>>>
>>> On Feb 4, 2010, at 12:41 AM, Arul Dhesiaseelan wrote:
>>>
>>>> I see from the javadocs that Jersey does not inject UriInfo from a constructor. Is there any other way to get access to the URIBuilder inside the constructor?
>>>>
>>>> -Arul
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>