title: Difference between ResourceConfig and ServletContextListener for Jersey Rest Service
body: <p>I want to initialize a Jersey Rest service and introduce a global application-wide variable which should be calculated at application start up-time and should be available in each rest resource and each method (here indicated by the integer globalAppValue=17, but will be a complex object later).</p>
  
  <p>In order to initialize the service and calculate the value once at start up I found two practices: The general ServletContextListener and the Jersey ResourceConfig method. But I have not understood what is the difference between both of them? Both methods fire at start up (both System.out-messages are printed).</p>
  
  <p>Here is the implementation of my ServletContextListener which works fine:</p>
  
  <pre><code>public class LoadConfigurationListener implements ServletContextListener
  {
      private int globalAppValue = 17;
  
      @Override
      public void contextDestroyed (ServletContextEvent event)
      {
      }
  
      @Override
      public void contextInitialized (ServletContextEvent event)
      {
          System.out.println ("ServletContext init.");
  
          ServletContext context = event.getServletContext ();
          context.setAttribute ("globalAppValue", globalAppValue);
      }
  }
  </code></pre>
  
  <p>And this is the implementation of the Jersey Rest ResourceConfig-method in which the ServletContext is not available. Neither is this Application-object later availabe by injection with the resource-methods:</p>
  
  <pre><code>@ApplicationPath("Resources")
  public class MyApplication extends ResourceConfig
  {
      @Context
      ServletContext context;
  
      private int globalAppValue = 17;
  
      public MyApplication () throws NamingException
      {
          System.out.println ("Application init.");
  
          // returns NullPointerException since ServletContext is not injected
          context.setAttribute ("globalAppValue", 17);
      }
  
      public int getAppValue ()
      {
          return globalAppValue;
      }
  }
  </code></pre>
  
  <p>This is the way I would like to gain access in the resource methods to the global value:</p>
  
  <pre><code>@Path("/")
  public class TestResource
  {
      @Context
      ServletContext context;
      @Context
      MyApplication application;
  
      @Path("/test")
      @GET
      public String sayHello () throws SQLException
      {
          String result = "Hello World: ";
  
          // returns NullPointerException since application is not injected
          result += "globalAppValue=" + application.getAppValue ();
  
          // works!
          result += "contextValue=" + context.getAttribute ("globalAppValue");
  
          return result;
      }
  }
  </code></pre>
  
  <p>So while the classic ServletContextListener works fine I got several problems to use the ResourceConfig/Application, but would prefer this way because it seems to integrate more natively into Jersey. So my question is which way would be the best practice to use. Thanks!</p>