Spring MVC, calling another controller from Inside

2020-04-02 06:55发布

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

@Autowired
private Controller controller.

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

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

6条回答
来,给爷笑一个
2楼-- · 2020-04-02 07:21

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.

查看更多
疯言疯语
3楼-- · 2020-04-02 07:22

If you do a call in between Controllers, 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 method as follow:

return "redirect:/yourDestinationControllerPath";
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-04-02 07:23

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-04-02 07:24

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.

查看更多
别忘想泡老子
6楼-- · 2020-04-02 07:29

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...

查看更多
孤傲高冷的网名
7楼-- · 2020-04-02 07:34

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 ?

查看更多
登录 后发表回答