title: Jersey Test + Spring - not injecting beans into Controllers
body: <p>Could someone tell me please how to get spring injection working on Controller classes registered in resource config:</p>
  
  <pre><code>ResourceConfig resourceConfig = new ResourceConfig(controllerClasses);
  </code></pre>
  
  <p>I include the spring context like this:</p>
  
  <pre><code>    private void mergeSpringContext(ResourceConfig resourceConfig) {
          ContextConfiguration annotation = getClass().getAnnotation(ContextConfiguration.class);
          if(annotation != null) {
              Class&lt;?&gt;[] contextClasses = annotation.classes();
  //            ApplicationContext context = new AnnotationConfigApplicationContext(contextClasses);
  //            resourceConfig.property("contextConfig", context);
              resourceConfig.registerClasses(contextClasses);
          }
      }
  </code></pre>
  
  <p>You can see two approaches there but neither works. Jersey-test always creates a new instance of the controller and spring can't know about it. Is there a way to make jersey use spring-instantiated controller (which is managed and will have all fields set) bean? Or do I have to get myself into trouble of using some AOP (@Configurable on the controller class).</p>
  
  <p>There is something called </p>
  
  <pre><code>jersey-spring3
  </code></pre>
  
  <p>and it's supposed to do it. And I think it does but not in the way I'd like it to - it searches for applicationContext.xml on my classpath. And this is not what I want because it is the test case which specifies which spring contexts to load.</p>
  
  <p>Thanks!</p>
  
  <p>// EDIT</p>
  
  <p>I managed to use spring-instantiated controllers in my application like this:</p>
  
  <pre><code>    private void mergeSpringContext(ResourceConfig resourceConfig) {
          ContextConfiguration annotation = getClass().getAnnotation(ContextConfiguration.class);
          if(annotation != null) {
              Class&lt;?&gt;[] contextClasses = annotation.classes();
              ApplicationContext context = new AnnotationConfigApplicationContext(contextClasses);
              Map&lt;String, Object&gt; beansOfType = context.getBeansWithAnnotation(Controller.class);
              Collection&lt;Object&gt; controllers = beansOfType.values();
              resourceConfig.registerInstances(controllers.toArray());
          }
      }
  </code></pre>
  
  <p>The problem with it is that I still don't have a clue how to put spring security in there...</p>