title: Spring MVC : autowiring a controller in another controller
body: <p>I have two controllers <code>LoginViewController</code> and <code>UserViewController</code> as </p>
  
  <pre><code>@Controller
  public class LoginViewController {
  
      @Autowired
      private UserViewController userViewController;  //Can't autowire, since spring creates a proxy for UserViewController class
  
      @RequestMapping(value="/login", method=POST)
      public String login(){
  
          //username and password checking etc
  
          if(login_successfull){
              //When login successfull, i need to redirect the screen to user dashboard  
              model.addAttribute("loginMessage", "You are loggined successfully")
              return userViewController.viewDashboard(userId);
          }   
      }
  }
  
  @Controller
  @RequestMapping("/user")
  public class UserViewController {
  
      @Autowired 
      private UserService userService;
  
      @RequestMapping(value="/dashboard", method=GET)
      public String viewDashboard(Model model, @RequestParam(value="id", required=true) Long userId){
          //Fetch and send user details to dashboard
          model.addAttribute("user", userService.get(userId));
          return "userDashboard"; 
      }
  }
  </code></pre>
  
  <p>After successful user login, I need to redirect the screen to user dashboard with a login success message. </p>
  
  <p>For this can use 2 approaches</p>
  
  <ol>
  <li><p>Since I have a method to load user dashboard in <code>UserViewController</code>, I autowired <code>UserViewController</code> in <code>LoginViewController</code>, which leads to <code>NoSuchBeanDefinitionException</code>, since spring creates a proxy for <code>UserViewController</code>.</p></li>
  <li><p>I can use <code>redirect/forward</code> to route to user dashboard as return <code>"redirect:/user/dashboard?id=123"</code>. But when I change the url of <code>viewDashboard()</code> method, I need to identify and correct all the <code>redirect/forward</code> statements.</p></li>
  </ol>
  
  <p>So, is there any way to invoke <code>UserViewController.viewDashboard()</code> from <code>LoginViewController</code> ?
  I am using spring 3.1.4 and thymeleaf</p>