title: Trouble creating a simple singleton class in Jersey 2 using built-in Jersey dependency injection
body: <p>I am having trouble getting a very basic implementation of a singleton class off the ground with Jersey 2 (2.7) and only Jersey's built-in HK2 dependency injection. I am running this on Tomcat.</p>
  
  <p>My goal is to create a singleton instance of a support class that will be used by various web service methods. I don't have a strong preference between constructor injection, method injection, and annotating a class member (as I do below).</p>
  
  <p>Here is my to-be-singleton class:</p>
  
  <pre class="lang-java prettyprint-override"><code>package singletest;
  
  import javax.inject.Singleton;
  
  @Singleton
  public class JustOne {
      private int secretNumber = 0;
  
      public void hitMe(int input) {
          secretNumber += input;
      }
  
      @Override
      public String toString() {
          return String.format("{ \"secretNumber\": %s }", secretNumber);
      }
  }
  </code></pre>
  
  <p>Here is my application class:</p>
  
  <pre class="lang-java prettyprint-override"><code>package singletest;
  
  import javax.ws.rs.ApplicationPath;
  import javax.ws.rs.core.Application;
  import java.util.HashSet;
  import java.util.Set;
  
  @ApplicationPath("/*")
  public class MainApplication extends Application {
      @Override
      public Set&lt;Class&lt;?&gt;&gt; getClasses() {
          Set&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;&gt;();
          classes.add(TestResource.class);
          return classes;
      }
  
      @Override
      public Set&lt;Object&gt; getSingletons() {
          Set&lt;Object&gt; singletons = new HashSet&lt;&gt;();
          singletons.add(new JustOneProvider());
          return singletons;
      }
  }
  </code></pre>
  
  <p>Here is my Provider/ContextResolver class:</p>
  
  <pre class="lang-java prettyprint-override"><code>package singletest;
  
  import javax.ws.rs.ext.ContextResolver;
  import javax.ws.rs.ext.Provider;
  
  @Provider
  public class JustOneProvider implements ContextResolver&lt;JustOne&gt; {
      private static JustOne justOne = new JustOne();
  
      @Override
      public JustOne getContext(Class&lt;?&gt; type) {
          return justOne;
      }
  }
  </code></pre>
  
  <p>web.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;singletest.MainApplication&lt;/filter-name&gt;
          &lt;filter-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
              &lt;param-value&gt;singletest.MainApplication&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;!-- pass to next filter if Jersey/App returns 404 --&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;jersey.config.servlet.filter.forwardOn404&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;singletest.MainApplication&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>The resource where I intend to inject the singleton instance of my JustOne class:</p>
  
  <pre class="lang-java prettyprint-override"><code>package singletest;
  
  import javax.inject.Inject;
  import javax.ws.rs.*;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  
  @Path("/test")
  public class TestResource {
  
      @Inject
      private JustOne justOne;
  
      @GET
      @Consumes(MediaType.TEXT_PLAIN)
      @Produces(MediaType.APPLICATION_JSON)
      @Path("/func1/{input}")
      public Response func1(@PathParam("input") int input) {
          justOne.hitMe(input);
          String responseData = justOne.toString();
          return Response.ok(responseData).build();
      }
  }
  </code></pre>
  
  <p>This warning is raised when the war is deployed/initialized in Jersey:</p>
  
  <pre><code>Apr 28, 2014 11:48:25 AM org.glassfish.jersey.server.ApplicationHandler initialize
  INFO: Initiating Jersey application, version Jersey: 2.7 ${buildNumber}...
  Apr 28, 2014 11:48:25 AM org.glassfish.jersey.internal.inject.Providers checkProviderRuntime
  WARNING: A provider java.lang.Class registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider java.lang.Class will be ignored.
  </code></pre>
  
  <p>And the error I get when I call this web service:</p>
  
  <pre><code>type Exception report
  
  message A MultiException has 3 exceptions. They are:
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception
  
  javax.servlet.ServletException: A MultiException has 3 exceptions.  They are:
  1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=JustOne,parent=TestResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,58952407)
  2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of singletest.TestResource errors were found
  3. java.lang.IllegalStateException: Unable to perform operation: resolve on singletest.TestResource
  
      org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:333)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  </code></pre>
  
  <p>So, I get it: hk2 isn't getting enough information to create/bind a JustOne instance and/or find/use my JustOneProvider. I'm sure I'm missing something basic. Any guidance would be appreciated.</p>