title: Spring mvc controller bean configuration
body: <p>I am working on a project using Spring mvc and I counter a problem that a handler is auto generated when I start the server.</p>
  
  <p>Here is the code:</p>
  
  <p>Controller</p>
  
  <pre><code>@Controller
  @RequestMapping(value="userstory/{projectid}/{sprintid}/")
  @SessionAttributes(value = "user")
  public class UserstoryController {
      private ISprintService sprintService;
      private IUserStoryService userStoryService;
      private IProjectService projectService;
      private IBurnDownChartService burnDownChartService;
      private ITaskService taskService;
  
      public void setSprintService(ISprintService sprintService) {
          this.sprintService = sprintService;
      }
  
      public void setUserStoryService(IUserStoryService userStoryService) {
          this.userStoryService = userStoryService;
      }
  
      public void setProjectService(IProjectService projectService) {
          this.projectService = projectService;
      }
  
      public void setBurnDownChartService(IBurnDownChartService burnDownChartService) {
          this.burnDownChartService = burnDownChartService;
      }
  
      public void setTaskService(ITaskService taskService) {
          this.taskService = taskService;
      }
  
      @RequestMapping(method=RequestMethod.GET)
      public String getUserstoryPage(@PathVariable("projectid") int pid, @PathVariable("sprintid") int sid,  @ModelAttribute("user") SerializablePerson user) {
          if (user.getId() != 0) {
              Project p = this.projectService.findProjectById(pid);
              if (p == null) {
                  throw new ResourceNotFoundException(pid);
              }
              if (user.getRole().equals("User")) {
                  return "userstory/user_userstory";
              }
              else {
                  return "userstory/admin_userstory";
              }
          }
          return "redirect:../../../login";
      }
  
  }
  </code></pre>
  
  <p>beans.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;
  &lt;beans&gt;
      &lt;bean id="personServiceImpl" class="ch.bsgroup.scrumit.service.impl.PersonServiceImpl" /&gt;
      &lt;bean id="projectServiceImpl" class="ch.bsgroup.scrumit.service.impl.ProjectServiceImpl" /&gt;
      &lt;bean id="sprintServiceImpl" class="ch.bsgroup.scrumit.service.impl.SprintServiceImpl" /&gt;
      &lt;bean id="userStoryServiceImpl" class="ch.bsgroup.scrumit.service.impl.UserStoryServiceImpl" /&gt;
      &lt;bean id="taskServiceImpl" class="ch.bsgroup.scrumit.service.impl.TaskServiceImpl" /&gt;
  
      &lt;bean id="projectService" class="ch.bsgroup.scrumit.service.impl.ProjectServiceImpl" /&gt;
      &lt;bean id="personService" class="ch.bsgroup.scrumit.service.impl.PersonServiceImpl" /&gt;
  
      &lt;bean id="sprintService" class="ch.bsgroup.scrumit.service.impl.SprintServiceImpl" /&gt;
      &lt;bean id="userStoryService" class="ch.bsgroup.scrumit.service.impl.UserStoryServiceImpl" /&gt;
      &lt;bean id="burnDownChartService" class="ch.bsgroup.scrumit.service.impl.BurnDownChartServiceImpl" /&gt;
      &lt;bean id="taskService" class="ch.bsgroup.scrumit.service.impl.TaskServiceImpl" /&gt;
  
      &lt;bean id="sprintUserstoryController" class="ch.bsgroup.scrumit.controller.SprintUserstoryController"&gt;
          &lt;property name="sprintService"&gt;
              &lt;ref local="sprintService"/&gt;
          &lt;/property&gt;
          &lt;property name="userStoryService"&gt;
              &lt;ref local="userStoryService"/&gt;
          &lt;/property&gt;
          &lt;property name="projectService"&gt;
              &lt;ref local="projectService"/&gt;
          &lt;/property&gt;
          &lt;property name="burnDownChartService"&gt;
              &lt;ref local="burnDownChartService"/&gt;
          &lt;/property&gt;
          &lt;property name="taskService"&gt;
              &lt;ref local="taskService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="boardController" class="ch.bsgroup.scrumit.controller.BoardController"&gt;
          &lt;property name="sprintService"&gt;
              &lt;ref local="sprintService"/&gt;
          &lt;/property&gt;
          &lt;property name="projectService"&gt;
              &lt;ref local="projectService"/&gt;
          &lt;/property&gt;
          &lt;property name="userStoryService"&gt;
              &lt;ref local="userStoryService"/&gt;
          &lt;/property&gt;
          &lt;property name="taskService"&gt;
              &lt;ref local="taskService"/&gt;
          &lt;/property&gt;
          &lt;property name="burnDownChartService"&gt;
              &lt;ref local="burnDownChartService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean name="register.jsp" id="registerController" class="ch.bsgroup.scrumit.controller.RegisterController"&gt;
          &lt;property name="personService"&gt;
              &lt;ref local="personService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="loginController" class="ch.bsgroup.scrumit.controller.LoginController" &gt;
          &lt;property name="personService"&gt;
              &lt;ref local="personService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="adminProjectController" class="ch.bsgroup.scrumit.controller.AdminProjectController"&gt;
          &lt;property name="projectService"&gt;
              &lt;ref local="projectService"/&gt;
          &lt;/property&gt;
          &lt;property name="personService"&gt;
              &lt;ref local="personService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="userProjectController" class="ch.bsgroup.scrumit.controller.UserProjectController"&gt;
          &lt;property name="projectService"&gt;
              &lt;ref local="projectService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="adminPersonController" class="ch.bsgroup.scrumit.controller.AdminPersonController"&gt;
          &lt;property name="projectService"&gt;
              &lt;ref local="projectService"/&gt;
          &lt;/property&gt;
          &lt;property name="personService"&gt;
              &lt;ref local="personService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="manageProjectMemberController" class="ch.bsgroup.scrumit.controller.ManageProjectMemberController"&gt;
          &lt;property name="projectService"&gt;
              &lt;ref local="projectService"/&gt;
          &lt;/property&gt;
          &lt;property name="personService"&gt;
              &lt;ref local="personService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="manageUserstoryController" class="ch.bsgroup.scrumit.controller.UserstoryController"&gt;
          &lt;property name="sprintService"&gt;
              &lt;ref local="sprintService"/&gt;
          &lt;/property&gt;
          &lt;property name="userStoryService"&gt;
              &lt;ref local="userStoryService"/&gt;
          &lt;/property&gt;
          &lt;property name="projectService"&gt;
              &lt;ref local="projectService"/&gt;
          &lt;/property&gt;
          &lt;property name="burnDownChartService"&gt;
              &lt;ref local="burnDownChartService"/&gt;
          &lt;/property&gt;
          &lt;property name="taskService"&gt;
              &lt;ref local="taskService"/&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I copy all the beans to show that there is no userstoryController mapped.</p>
  
  <p>web.xml</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_2_5.xsd" version="2.5"&gt;
    &lt;filter&gt;
      &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
      &lt;init-param&gt;
        &lt;param-name&gt;encoding&lt;/param-name&gt;
        &lt;param-value&gt;UTF-8&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
        &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
        &lt;param-value&gt;true&lt;/param-value&gt;
      &lt;/init-param&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
      &lt;filter-name&gt;characterEncodingFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;
    &lt;filter&gt;
      &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.tuckey.web.filters.urlrewrite.UrlRewriteFilter&lt;/filter-class&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
      &lt;filter-name&gt;UrlRewriteFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;scrumit&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
        &lt;param-value&gt;/WEB-INF/spring/app-config.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;scrumit&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/app/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>app-config.xml</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:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="
          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;!-- Scans the classpath of this application for @Components to deploy as beans --&gt;
      &lt;context:component-scan base-package="ch.bsgroup.scrumit.controller" /&gt;
  
      &lt;!-- Application Message Bundle --&gt;
      &lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="/WEB-INF/messages/messages" /&gt;
          &lt;property name="cacheSeconds" value="0" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Including beans --&gt;
      &lt;import resource="beans.xml" /&gt;
  
      &lt;!-- Configures Spring MVC --&gt;
      &lt;import resource="mvc-config.xml" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And this is what I have when I start the server:</p>
  
  <pre><code>INFO : org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapped URL path [/userstory/{projectid}/{sprintid}/] onto handler 'userstoryController'
  </code></pre>
  
  <p>The error is:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'manageUserstoryController' to URL path [/userstory/{projectid}/{sprintid}/]: There is already handler of type [class ch.bsgroup.scrumit.controller.UserstoryController] mapped.
  </code></pre>
  
  <p>The program does not allow me to map UserstoryController to manageUserstoryController because of the existence of userstoryController.</p>
  
  <p>Could anyone explain to me why userstoryController is generated, and how to map it properly.
  Thanks in advance.</p>