title: Inject Spring context to Jersey server manually
body: <p>I want to use Jersey with Spring. I'm developing standalone application (not being deployed to some webserver, but running as a console app). So I made something like this:</p>
  
  <pre><code>public static void main(String[] args) throws IOException {
      ResourceConfig rc = new PackagesResourceConfig(MAIN_PACKEAGE);
      HttpServerFactory.create(BASE_URI, rc).start();
  }
  </code></pre>
  
  <p>That works pretty well, but next I try to integrate Jersey Resources with Spring. As it's mentioned <a href="https://jersey.java.net/nonav/documentation/latest/spring.html" rel="nofollow">here</a> I should add <strong>jersey-spring3</strong> to classpath, so I add a dependency to pom.xml:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
      &lt;artifactId&gt;jersey-spring3&lt;/artifactId&gt;
      &lt;version&gt;2.17&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>But it doesn't actually work: it doesn't even initialize the Spring context (referring the logs). I've tried to initialize SC manually:</p>
  
  <pre><code>new ClassPathXmlApplicationContext(PATH_TO_APP_CONTEXT);
  </code></pre>
  
  <p>And this worked, but separately: context is being initialized, but the Dependency Injection is not working - I can't autowire some bean to Jersey Resource (also marked as Spring Component).</p>
  
  <p><strong>Update</strong></p>
  
  <p>I figured out that <strong>jersey-spring3</strong> works great with Grizzly server. So the working code sample is:</p>
  
  <pre><code>public static void main(String[] args) throws IOException {
      ResourceConfig rc = new ResourceConfig().packages(MAIN_PACKAGE);
      GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
  }
  </code></pre>
  
  <p>But I still wonder why integration with Spring  doesn't work if I create server using <code>HttpServerFactory.create(BASE_URI, rc)</code>...</p>