Take the 2-minute tour ×

I am working on an existing code that is using one controller to call a method on the 2nd controller. There are 2 implementations I have seen so far.

1st Implementation

return new Controller().method(request, response);

2nd Implementation

private Controller controller.

return this.controller.method(request, response);

Which is the right implementation, what are the problems if any with either of them.

share|improve this question
What is the advantage of this? Doing so breaks a lot of convention, and you should be considering revising, i.e. breaking up the controller code and maybe moving some of the logic into the business layer. –  dardo Oct 11 '12 at 21:36
add comment

6 Answers

up vote 3 down vote accepted

The mere fact that you need to call a method from another controller reveals a probable design flaw.

With option 1, you lose everything the Spring DI container brought you: namely, that other controller may be instantiated by Spring with some other dependencies wired into it. If you instantiate it yourself, even if it does work at this moment, because you probably have no @Autowired / @Value dependencies, it will break once you'll add dependencies on other resource(s). Besides, you already have an instance the container built for you, why create others ?

share|improve this answer
add comment

Sounds like you need to refactor the code. Extract what is common between the two controllers into a separate class, then call that from either controller.

share|improve this answer
+1 for good practice, and for being from Milwaukee, and for basically reiterating my comment. –  dardo Oct 11 '12 at 21:48
add comment

The first needs more work, firstly do you really want to create a new instance of Controller class everytime?

The second uses a pattern known as dependency injection or inversion of control which is nicer. Let spring manage scope of the bean for you, by default it will create only 1 instance of the Controller class however if at some point (for some reason) you don't want this behaviour its straightforward to create many instances...

share|improve this answer
add comment

You are doing completely wrong. See Costi Ciudatu's answer for what is wrong.

Solution: I advice you to have service layer and dao layer classes associated with your controllers. Suppose you have AccountController, you will have AccountService class(interface + implementation) and AccountDao(interface + implementation).

Now if user logs in (LoginController) and you need account so you will autowire AccountService in LoginController, you will get user's account details from AccountService methods.

share|improve this answer
add comment

if you do a call in between controller you either there is a flaw or you want to make a redirection which is totally valid. If redirection is the case just return in your controller code as follow.

return "redirect:/yourDestinationControllerPath.htm";
share|improve this answer
add comment

The second one is correct because you will not make an instance of it every time. The @Autowired annotation injects the object into your code when needed.

Check this http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/beans/factory/annotation/Autowired.html

But there is a @Controller annotation and you should be using that for controllers and @Service for beans you want to autowire.

share|improve this answer
add comment

Your Answer


