title: Spring autowiring fails in RESTeasy service
body: <p>I have a simple service which fails to autowire bean.
  Although getting the same bean through context succeeds.
  So the bean creation and registration in repository is working, but autowiring does not.
  Changing class of the field in the service (<code>MyRepository</code> -> <code>YourRepository</code>), there is an error thrown that such bean does not exists, so the autowiring mechanism is working.</p>
  
  <p>Any ideas what might be missing?</p>
  
  <pre><code>@Component
  @Path("/")
  public class RestService {
  
      @Autowired 
      private MyRepository myRepository; // is not autowired and is null
  
      @GET
      @Path("/{param}")
      @Produces(MediaType.APPLICATION_JSON)
      public Response printMessage(@PathParam("param") String msg) {
          return Response.ok(
      AppContext.getContext().getBean("myRepository") == myRepository)
                .build(); // false
      }
  
      public void setMyRepository(MyRepository myRepository) {
          this.myRepository = myRepository;
      }
  }
  </code></pre>
  
  <p><code>AppContext</code> above is my simple implementation of <code>ApplicationContextAware</code></p>
  
  <p>Repository</p>
  
  <pre><code>@Repository
  public interface MyRepository extends MongoRepository&lt;MyEntity, String&gt; {
  
  }
  </code></pre>
  
  <p>There is no .xml configuration and spring is initialized through </p>
  
  <pre><code>public class MyInitializer implements WebApplicationInitializer {
  
      @Override
      public void onStartup(ServletContext container) throws ServletException {
          container.addListener(new ResteasyBootstrap());
  
          final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
          ContextLoaderListener springListener = new ContextLoaderListener(rootContext);
          rootContext.register(MyConfiguration.class);
          container.addListener(springListener);
      }
  }
  </code></pre>
  
  <p>And configuration class</p>
  
  <pre><code>@Configuration
  @EnableMongoRepositories("my.package.repository")
  @ComponentScan("my.package")
  public class MyConfiguration {
  
      @Bean
      public MongoTemplate mongoTemplate() throws UnknownHostException {
          return new MongoTemplate(new MongoClient("localhost"), "db");
      }
  }
  </code></pre>
  
  <p><strong>EDIT after 2 comments</strong></p>
  
  <p>I'm using the following library for RESTeasy - spring integration.
  Do I need some other?</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
          &lt;artifactId&gt;resteasy-spring&lt;/artifactId&gt;
          &lt;version&gt;3.0.8.Final&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>I think JAX-RS and RESTeasy is working correctly, because service is working and I can access it through web when deployed on JBoss</p>
  
  <p><strong>EDIT for workaround</strong></p>
  
  <p>Service is initialized correctly if I create the following constructor, but it feels more like a workaround</p>
  
  <pre><code>public RestService() {
      this.myRepository = MyContext.getContext().getBean(MyRepository.class);
  }
  </code></pre>
  
  <p>And MyContext class for more clarity</p>
  
  <pre><code>@Component
  public class MyContext implements ApplicationContextAware {
      private static ApplicationContext context;
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
          context = applicationContext;   
      }
  
      public static ApplicationContext getContext() {
          return context;
      }
  }
  </code></pre>