users@jersey.java.net

Re: [Jersey] injecting UriInfo in constructor

From: Arul Dhesiaseelan <arul_at_fluxcorp.com>
Date: Thu, 4 Feb 2010 09:24:04 -0700

Thanks Paul.

Btw, I am not using any of those injection frameworks in this case. 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
>