title: Dependency Injection in Spring MVC web-application
body: <p>I am trying to understand how to do DI in a web-application based on Spring MVC framework.</p>
  
  <p>For this, I modified the <strong>web.xml</strong>.</p>
  
  <pre><code>&lt;web-app ...&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;spring-dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring-dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Then, I created <strong>spring-dispatcher-servlet.xml</strong>:</p>
  
  <pre><code>&lt;beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"&gt;
  
      &lt;bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/&gt;
  
      &lt;bean name="/welcome.html" class="com.example.hellocontroller.HelloController"/&gt;
  
      &lt;bean id="simpleUtil" class="com.example.util.SimpleUtil"/&gt; -----&gt;(1)
  
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/&lt;/value&gt;
          &lt;/property&gt;
  
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>The below is the controller class:</p>
  
  <pre><code>public class HelloController extends AbstractController {
  
      @Autowired
      private SimpleUtil simpleUtil; -------&gt; (2)
  
      @Override
      protected ModelAndView handleRequestInternal(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
  
          System.out.println("calling the method add(a,b) ");
  
          simpleUtil.add(2, 3);
  
          ModelAndView modelandview = new ModelAndView("HelloPage");
          modelandview.addObject("welcomeMessage", "first Spring MVC Application");
          return modelandview;
      }
  }
  </code></pre>
  
  <p>In spring-dispatcher-servlet.xml I am defining various beans, in particular at (1) --> <code>&lt;bean name="simpleUtil" class="com.example.util.SimpleUtil"/&gt;</code></p>
  
  <p>Now, in the controller I want this bean to be injected by Spring framework as listed at (2):</p>
  
  <pre><code>    @Autowired
      private SimpleUtil simpleUtil;
  </code></pre>
  
  <p>When I run this program I am getting NullPointerException, meaning that Spring didn't injected the dependency.</p>
  
  <p>Is there something wrong I am doing here? How to make this work?</p>
  
  <p>I have few related questions here:</p>
  
  <p>Q1) Does <code>spring-dispatcher-servlet.xml</code> serve as bean definition configuration file? If not, then where do I provide the bean definition files which I want to be managed by Spring?</p>
  
  <p>Q2) How does Spring MVC's front-controller gets to know what bean id of Handling Mapping is? (in our case the ID given is "HandlingMapping"). </p>