title: How do I access a custom bean in Spring MVC controller class?
body: <p>I'm new to Spring so please bear with me.</p>
  
  <p>I was looking at this guide (http://java2t.com/233/using-spring3-jdbctemplate-rowmapper-to-return-list-of-records/).
  It uses a "normal" java project in eclipse. I'm trying to simulate the example using dynamic web project but I'm not having any success.</p>
  
  <p><strong>my web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      id="WebApp_ID"
      version="3.0"&gt;
  
      &lt;display-name&gt;StaffDirectory&lt;/display-name&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;    
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>my spring-servlet.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:component-scan base-package="org.flinders.staff.directory" /&gt;
  
      &lt;mvc:annotation-driven /&gt;
      &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
      &lt;mvc:view-controller path="/" view-name="index"  /&gt; 
  
      &lt;bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/test" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="PVHmg57bE4" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
       &lt;bean id="staffDirectoryDAO" class="org.flinders.staff.directory.dao.impl.StaffDirectoryDAOImpl"&gt;
          &lt;property name="jdbcTemplate" ref="jdbcTemplate" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="staffDirectoryService" class="org.flinders.staff.directory.services.impl.StaffDirectoryServiceImpl"&gt;
          &lt;property name="staffDirectoryDAO" ref="staffDirectoryDAO" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass" &gt;
              &lt;value&gt;org.springframework.web.servlet.view.tiles2.TilesView&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"&gt;
          &lt;property name="definitions"&gt;
              &lt;list&gt;
                  &lt;value&gt;/WEB-INF/tiles.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>my controller class</strong></p>
  
  <pre><code>package org.flinders.staff.directory.controllers;
  
  import java.util.List;
  
  import org.flinders.staff.directory.models.database.StaffModel;
  import org.flinders.staff.directory.models.misc.SearchModel;
  import org.flinders.staff.directory.services.StaffDirectoryService;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.servlet.ModelAndView;
  
  @Controller
  public class StaffDirectoryController {
      private StaffDirectoryService staffDirectoryService;
  
      @RequestMapping("/SearchResults")
      public void showSearchResults() {
          //StaffDirectoryService staffDirectoryService = (StaffDirectoryService) getServletContext().getBean("StaffDirectoryService");
          List&lt;StaffModel&gt; staffList = staffDirectoryService.viewStaffResults();
  
          for (StaffModel staffModel : staffList) {
  
            System.out.println(staffModel.getStaffID() + "    : "
  
                + staffModel.getFirstname() + "  : " + staffModel.getSurname());
  
          }
  
          System.out.println();
  
      }
  
      @RequestMapping("/SearchForm")
      public ModelAndView showSearchForm() {
          return new ModelAndView("search/SearchForm", "SearchModel", new SearchModel());
      }
  }
  </code></pre>
  
  <p>I don't encounter any publishing/restarting my Tomcat instance. But going to my browser, my code
  throws an exception on this code (List staffList = staffDirectoryService.viewStaffResults();) in my controller class.</p>
  
  <p>Any ideas? Thank you! :)</p>