title: Using JSR-330 annotations with Jersey 2 + Spring
body: <p>I have been trying to use Spring 4 with Jersey 2 and notice that using the jersey-spring3 extension there is no way to make <code>@Named</code> annotated resources managed by Spring. They must be annotated with <code>@Component</code> to be managed by Spring. </p>
  
  <p>If a resource is annotated with <code>@Named</code> HK2 seems to "take over" managing that resource. </p>
  
  <p><strong>Resource</strong></p>
  
  <pre><code>import com.example.jersey.spring.Greeting;
  import java.util.logging.Logger;
  import javax.inject.Inject;
  import javax.inject.Named;
  import javax.inject.Singleton;
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  @Named
  @Singleton
  @Path("resource")
  public class Resource {
  
      private static final Logger logger = Logger.getLogger("Resource");
  
      public void init() {
          logger.info("Creating -&gt; " + this + " injected with -&gt; " + greeting);
      }
  
      @Inject
      private Greeting greeting;
  
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String getIt() {
          logger.info("Working on " + this + " Greeting " + greeting);
          return "Got it!";
      }
  }
  </code></pre>
  
  <p>With the following Spring applicationContext</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" 
     default-init-method="init"&gt;
  
      &lt;context:component-scan base-package="com.example.jersey.webapp"/&gt;
      &lt;bean id="greeting" class="com.example.jersey.spring.Greeting"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>Results in the following log messages being printed on start-up</p>
  
  <pre><code>Oct 18, 2017 3:11:44 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor &lt;init&gt;
  INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
  Oct 18, 2017 3:11:44 PM com.example.jersey.webapp.Resource init
  INFO: Creating -&gt; com.example.jersey.webapp.Resource@6e199bab injected with -&gt; com.example.jersey.spring.Greeting@533b3005
  Oct 18, 2017 3:11:45 PM org.springframework.web.context.ContextLoader initWebApplicationContext
  INFO: Root WebApplicationContext: initialization completed in 249 ms
  </code></pre>
  
  <p>And after a couple of GET calls.</p>
  
  <pre><code>Oct 18, 2017 3:11:56 PM com.example.jersey.webapp.Resource getIt
  INFO: Working on com.example.jersey.webapp.Resource@40328bbc Greeting com.example.jersey.spring.Greeting@533b3005
  Oct 18, 2017 3:12:03 PM com.example.jersey.webapp.Resource getIt
  INFO: Working on com.example.jersey.webapp.Resource@40328bbc Greeting com.example.jersey.spring.Greeting@533b3005
  </code></pre>
  
  <p>So it appears Spring does create the bean but that is not the one thats used to service the requests. However if instead of <code>Named</code>, <code>Component</code> is used the entire lifecycle is managed by Spring.</p>
  
  <p>Is there a way to use the standard <code>@Named</code> annotation and ensure Spring manages the lifecycle of resource completely?</p>