title: Not able to load servlet context in Spring
body: <p>I'm trying to load the properties data on server startup. For this I have created a class that implements <code>ServletContextListener</code>. This will load all the properties.</p>
  
  <p>And in my DaoImpl class, I'm trying to get the properties data and get initialized to some of the strings. But it throws the exception </p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'bbService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bbService': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BBDAO' defined in ServletContext resource [/WEB-INF/spring-web-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.bb.dao.BBDaoImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
  </code></pre>
  
  <p>Here is my Config class</p>
  
  <pre><code>public class Config implements ServletContextListener {
  
  private static final String ATTRIBUTE_NAME = "config";
  private Properties config = new Properties();
  
  @Override
  public void contextInitialized(ServletContextEvent event){
      try {
          Resource resource = new ClassPathResource("/uat.properties");
          config = PropertiesLoaderUtils.loadProperties(resource);
      } catch (IOException e) {
          try {
              throw new QiibException("Loading config failed");
          } catch (QiibException e1) {
              e1.printStackTrace();
          }
      }
      event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
  }
  
  @Override
  public void contextDestroyed(ServletContextEvent event) {
  }
  
  public static Config getInstance(ServletContext context) {
      return (Config) context.getAttribute(ATTRIBUTE_NAME);
  }
  
  public String getProperty(String key) {
      return config.getProperty(key);
  }
  }
  </code></pre>
  
  <p>The DAOImpl class is </p>
  
  <pre><code>public class BBDaoImpl extends JdbcDaoSupport implements BBDao, ServletContextAware  {
  
  Properties properties = null;
  
  @Autowired
  private ServletContext ctx; 
  
  
  public void setServletContext(ServletContext servletContext) {
      this.ctx = servletContext;
  }
  
  public BBDaoImpl() throws IOException {
      super();
      Config config = Config.getInstance(ctx); --&gt; ctx is null here. 
  </code></pre>
  
  <p>What is wrong here? </p>
  
  <p>Any ideas would be greatly appreciated. </p>