title: Injecting into a Jersey Resource class
body: <p>I did try going through the following links 
  <a href="https://stackoverflow.com/questions/7121445/how-to-wire-in-a-collaborator-into-a-jersey-resource">How to wire in a collaborator into a Jersey resource?</a>
  and
  <a href="https://stackoverflow.com/questions/7510874/access-external-objects-in-jersey-resource-class">Access external objects in Jersey Resource class</a>
  But still i am unable to find a working sample which shows how to inject into a Resource class.
  I am not using Spring or a web container.</p>
  
  <p><strong>My Resource is</strong></p>
  
  <pre><code>package resource;
  
  import javax.ws.rs.FormParam;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  
  @Path("/something")
  public class Resource
  {
      @MyResource
      Integer foo = null;
      private static String response = "SampleData from Resource";
  
      public Resource()
      {
          System.out.println("...constructor called :" + foo);
      }
  
      @Path("/that")
      @GET
      @Produces("text/plain")
      public String sendResponse()
      {
          return response + "\n";
      }
  }
  </code></pre>
  
  <p><strong>My Provider is</strong></p>
  
  <pre><code>package resource;
  
  import javax.ws.rs.ext.Provider;
  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;
  
  @Provider
  public class MyResourceProvider implements InjectableProvider&lt;MyResource, Integer&gt;
  {
      @Override
      public ComponentScope getScope()
      {
         return ComponentScope.PerRequest;
      }
  
       @Override
      public Injectable getInjectable(final ComponentContext arg0, final MyResource arg1, final Integer arg2)
      {
         return new Injectable&lt;Object&gt;()
          {
              @Override
              public Object getValue()
              {
                return new Integer(99);
              }
          };
      }
  }
  </code></pre>
  
  <p><strong>My EndpointPublisher is</strong></p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  import javax.ws.rs.core.MediaType;
  import com.sun.jersey.api.client.Client;
  import com.sun.jersey.api.client.ClientResponse;
  import com.sun.jersey.api.client.WebResource;
  import com.sun.jersey.api.container.grizzly.GrizzlyWebContainerFactory;
  
  class EndpointPublisher
  {
      public static void main(final String[] args)
      {
  
          final String address = "http://localhost:8080/";
          final Map&lt;String, String&gt; config = new HashMap&lt;String, String&gt;();
          config.put("com.sun.jersey.config.property.packages", "resource");
          try
          {
              GrizzlyWebContainerFactory.create(address, config);
              System.out.println("server started ....." + address);
              callGet();
          }
          catch (final Exception e)
          {
              e.printStackTrace();
          }
      }
  
      public static void callGet()
      {
          Client client = null;
          ClientResponse response = null;
          client = Client.create();
          final WebResource resource =
                  client.resource("http://localhost:8080/something");
          response = resource.path("that")
                  .accept(MediaType.TEXT_XML_TYPE, MediaType.APPLICATION_XML_TYPE)
                  .type(MediaType.TEXT_XML)
                  .get(ClientResponse.class);
          System.out.println("&gt;&gt;&gt;&gt; " + response.getResponseDate());
      }
  }
  </code></pre>
  
  <p><strong>My annotation being</strong></p>
  
  <pre><code>@Retention(RetentionPolicy.RUNTIME)
  public @interface MyResource
  {}
  </code></pre>
  
  <p>But when i execute my EndpointPublisher i am unable to inject foo!!</p>