title: Store custom data maps as attributes within ServletContext?
body: <p>I was looking for a way to load some data from DB into a HashMap and make that map available for all root resource classes and some additional provider classes (implementation of ContainerRequestFilter to be more precise). I found and implemented a solution where I execute a query and load the map within a contextInitialized method of ServletContextListener and set it as an ServletContext attribute:</p>
  
  <pre><code>@WebListener
  public class ServletContextCaching implements ServletContextListener {
  
      @Inject
      private SomeBeanLocalInterface someBean;
  
      @Override
      public void contextDestroyed(ServletContextEvent sce) {
          ServletContext context = sce.getServletContext();
          context.removeAttribute("CUSTOM_DATA_MAP");
  
      }
  
      @Override
      public void contextInitialized(ServletContextEvent sce) {
  
          List&lt;SomeEntity&gt; someEntities = someBean.findAllActive();
          Map&lt;String, String&gt; temp = new HashMap&lt;String, String&gt;();
          for (SomeEntity e : someEntities) {
              temp.put(e.getSomeKey(), e.getSomeValue());
          }
          ServletContext context = sce.getServletContext();
          context.setAttribute("CUSTOM_DATA_MAP", temp);
  
      }
  
  }
  </code></pre>
  
  <p>Is it good practice to store custom data (i.e. attributes) within servlet context and thus make it available for "everyone"? What is the suggested practice for loading some data from DB into memory maps within REST services? I am pretty sure I am not the first one needing this :)</p>