title: Dependency injection with Jersey 2.0
body: <p>Starting from scratch without any previous Jersey 1.x knowledge, I'm having a hard time understanding how to setup dependency injection in my Jersey 2.0 project.  </p>
  
  <p>I also understand that HK2 is available in Jersey 2.0, but I cannot seem to find docs that help with Jersey 2.0 integration.</p>
  
  <pre class="lang-java prettyprint-override"><code>@ManagedBean
  @Path("myresource")
  public class MyResource {
  
      @Inject
      MyService myService;
  
      /**
       * Method handling HTTP GET requests. The returned object will be sent
       * to the client as "text/plain" media type.
       *
       * @return String that will be returned as a text/plain response.
       */
      @GET
      @Produces(MediaType.APPLICATION_JSON)
      @Path("/getit")
      public String getIt() {
          return "Got it {" + myService + "}";
      }
  }
  
  @Resource
  @ManagedBean
  public class MyService {
      void serviceCall() {
          System.out.print("Service calls");
      }
  }
  </code></pre>
  
  <p>pom.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;properties&gt;
      &lt;jersey.version&gt;2.0-rc1&lt;/jersey.version&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  &lt;/properties&gt;
  
  &lt;dependencyManagement&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-bom&lt;/artifactId&gt;
              &lt;version&gt;${jersey.version}&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-common&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey&lt;/groupId&gt;
          &lt;artifactId&gt;jax-rs-ri&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>I can get the container to start and serve up my resource, but as soon as I add @Inject to MyService, the framework throws an exception:</p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [com.noip.MyApplication] in context with path [/jaxrs] threw exception [A MultiException has 3 exceptions.  They are:
  1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MyService,parent=MyResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1039471128)
  2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of com.noip.MyResource errors were found
  3. java.lang.IllegalStateException: Unable to perform operation: resolve on com.noip.MyResource
  ] with root cause
  org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=MyService,parent=MyResource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1039471128)
      at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
  </code></pre>
  
  <p><br>
  My starter project is available at GitHub: <a href="https://github.com/donaldjarmstrong/jaxrs" rel="noreferrer">https://github.com/donaldjarmstrong/jaxrs</a></p>