title: Constructor Dependency Injection in Spring MVC 3
body: <p>I'm trying to do a dependency injection in my controller, instead using setters methods, but it didnot work.</p>
  
  <p>What I want: just bind the domain model with my form, in a controller class using springframework 3.1.</p>
  
  <p>My domain class:</p>
  
  <pre><code>public class Person {
  
    private String name;
  
    @Autowired
    public Person(String name) {
      this.name = name;
    }
  
    public String getName() {
      return this.name;
    }
  
  }
  </code></pre>
  
  <p>My jsp(person-get):</p>
  
  <pre><code>&lt;form action="test-person" method="post"&gt;
  &lt;table cellpadding="0" cellspacing="0" align="center"&gt;
      &lt;tr&gt;
          &lt;td&gt; &lt;b&gt; NAME: &lt;/b&gt; &lt;/td&gt;
          &lt;td&gt; &lt;input type="text" id="name" name="name"&gt; &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt; 
  &lt;/form&gt;
  </code></pre>
  
  <p>My Controller:</p>
  
  <pre><code>@Controller
  public class PersonController {
  
  @RequestMapping(value = "test-person", method = RequestMethod.GET)
  public String loadViewGet() {       
      return "person-get";
  }
  
  @RequestMapping(value = "test-person", method = RequestMethod.POST)
  public String doPost(Person person, Model model) {
  
      model.addAttribute("person", person);
  
      return "person-post";
  }
  
  }
  </code></pre>
  
  <p>And the last jsp showing the entered name of the person by the user (person-post):</p>
  
  <pre><code>&lt;table cellpadding="0" cellspacing="0" align="center"&gt;
      &lt;tr&gt;
          &lt;td&gt; &lt;b&gt; NAME: &lt;/b&gt; &lt;/td&gt;
          &lt;td&gt; ${person.name} &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt; 
  </code></pre>
  
  <p>The throwed exception:</p>
  
  <pre><code>java.lang.NoSuchMethodException: models.Person.&lt;init&gt;()
  java.lang.Class.getConstructor0(Unknown Source)
  java.lang.Class.getDeclaredConstructor(Unknown Source)
  org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
  org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttri    bute(ModelAttributeMethodProcessor.java:132)
  org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodPr    ocessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
  org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgu    ment(ModelAttributeMethodProcessor.java:103)
  org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resol    veArgument(HandlerMethodArgumentResolverComposite.java:75)
  org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValu    es(InvocableHandlerMethod.java:156)
  org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(Invo    cableHandlerMethod.java:117)
  org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod    .invokeAndHandle(ServletInvocableHandlerMethod.java:96)
  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.    invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.    handleInternal(RequestMappingHandlerAdapter.java:578)
  org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(Abst    ractHandlerMethodAdapter.java:80)
  org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java    :923)
  org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:    852)
  org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.ja    va:882)
  org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  br.gov.sp.mococa.filtros.FiltroDeConexao.doFilter(FiltroDeConexao.java:31)
  </code></pre>
  
  <p>I think that the spring is looking for a default constructor whitout parameters. Why it is hapenning?</p>
  
  <p>Thanks!</p>