title: load the value from database when the server is started
body: <p>I am working on spring and servlet based application.
  My requirement is to load the data from database and store in ehcache.I have written a listener which is called once the jboss server is started. But the issue is in my application we are using spring hibernate classes to connect to database and load the data from DB. 
  As per the requirement data has to be retrieved from database and store in cache object(ehcache). But when listener class is loaded other xml configuration files(applicationContext.xml..) are not yet loaded where we configure datasource details to connect to database. Below is my code:</p>
  
  <p>Listener class.</p>
  
  <pre><code>import net.sf.ehcache.*;
  import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  /imports
  public class MyInitializationListener extends HibernateDaoSupport implements ServletContextListener {
  
      /** Singleton instance of CacheManager. */
      private static CacheManager singletonManager = null;
  
      public void contextDestroyed(ServletContextEvent arg0) {
          System.out.println("--ServletContextListener destroyed*--");
      }
      private static CacheManager getInstance() {
          if (singletonManager == null) {
              singletonManager = CacheManager.create();
          }
          return singletonManager;
      }
  
      private Cache getCache() {
          Cache cache = null;
          cache = MyInitializationListener.getInstance().getCache("myCache");
          return cache;
      }
      // Run this before web application is started
  
      public void contextInitialized(ServletContextEvent arg0) {
      final Cache cache = getCache();
          final String dbValue = getDBValue();
          //logic here
           }
  public String getDBValue() throws DataLayerException{
      //logic to get the value from database
  
  }
  </code></pre>
  
  <p>Below is the exception:</p>
  
  <pre><code>ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/eas-webservice/ivr]] Exception sending context initialized event to listener instance of class com.data.listener.MyInitializationListener
  java.lang.NullPointerException
  </code></pre>
  
  <p>Did any one face this situation before.What would be the best possible way to connect to database and get the values from database and store in cache when the server is started. Suggestions would be helpful.</p>
  
  <p>--EDITED--</p>
  
  <p>I tried the below way using @PostConstruct to a method to call loadData() when the server is started.But when I checked in log file print statement was not there which states that the method is not being called when the server was started.</p>
  
  <pre><code>@Component
  public class MyDataStore {
      @PostConstruct
      public void loadData()
      {
          System.out.println("--In @postconstruct, loadData-");
      }
  }
  </code></pre>