title: @Context not injecting ServletContext in RESTEasy JAX-RS application
body: <p>I am deploying a JAX-RS application to JBoss EAP 6.2.
  I am trying to obtain a <code>ServletContext</code> from inside of a JAX-RS resource class so that I can read some <code>context-param</code> values that I have set in the <code>WEB-INF/web.xml</code> file.
  I.e., after I've gotten hold of the <code>ServletContext</code> I was planning to call <code>ServletContext#getInitParam</code> to get the value.
  I am using injection to get the <code>ServletContext</code> as advised <a href="https://stackoverflow.com/a/1814788/274677">here</a>.</p>
  
  <p>The relevant portion of my <code>web.xml</code> is:</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;resteasy-servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;
          org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
      &lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
          &lt;param-value&gt;foo.MyApplication&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;resteasy-servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/jax-rs/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>So I am using RESTEasy which is bundled with JBoss.</p>
  
  <p>Class <code>MyApplication</code> is:</p>
  
  <pre><code>public class MyApplication extends Application {
  
      private Set&lt;Object&gt; singletons = new HashSet&lt;&gt;();
  
      public MyApplication() {
          singletons.add( new MyResource() );
      }
  
      @Override
      public Set&lt;Object&gt; getSingletons() {
          return singletons;
      }
  }
  </code></pre>
  
  <p>&hellip; and finally in class <code>MyResource</code> I have the following:</p>
  
  <pre><code>@Path(...)
  public class MyResource {
  
      @Context
      ServletContext context;
  
      public MyResource() {
          // I understand context is supposed to be null here
      }
  
      // ... but it should have been injected by the time we reach the service method.
      @Path("/somePath")
      @GET
      @Produces(MediaType.APPLICATION_JSON) 
      public Response someMethod( ) {
          if (context==null) throw new RuntimeException();
          ...
      }
  }
  </code></pre>
  
  <p>The above code always results in the <code>RuntimeException</code> being thrown. I.e. RESTEasy somehow fails to inject the <code>ServletContext</code>. Note that I don't have any other JAX-RS problems. I.e. if I hardcode the <code>context-param</code> values I was hoping to be able to retrieve via `ServletContext#getInitParameter", then the JAX-RS rest functionality works as expected when the WAR is deployed to JBoss.</p>
  
  <p>Experimenting further I discovered that the <code>ServletContext</code>is only injected if I perform the injection at an argument of the service method like this:</p>
  
  <pre><code>@Path("/somePath")
  @GET
  @Produces(MediaType.APPLICATION_JSON) 
  public Response someMethod(@Context ServletContext servletContext) {
      ...
  }
  </code></pre>
  
  <p>&hellip; however I would prefer not to change the API. Moreover, I would like to perform some costly initialization based on the <code>context-param</code> value once and for all, not on every service method invocation.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li>why is the injection failing?</li>
  <li>I am a little tired of the annotations magic failing at runtime, is there a way to get the <code>ServletContext</code> without using annotations?</li>
  <li>Alternatively, is it possible for my <code>MyApplication</code> class to obtain the <code>ServletContext</code> and pass it to the <code>MyResource</code> class as a constructor parameter?</li>
  <li>If all else fails I guess I can always read and parse the web.xml file myself using <code>Class#getResourceAsStream</code> ?</li>
  </ol>