Spring MVC中的Controller是Serlvet嗎?


1. Controller不是Servlet

DispatcherServler是Spring MVC中的唯一Servlet,(這點可通過查看FrameworkServlet的子類確認)
Servlet容器(Tomcat)把所有請求都轉發到DispatcherServler
然后通過HandlerMapping把請求路由到具體的Controller中。
因此,Controller只是一個普通的JavaBean
官網文檔對@Controller注解的解釋

The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API. 
However, you can still reference Servlet-specific features if you need to.

2. DispatcherServlet由誰創建

DispatcherServler的生命周期與普通的Servlet一樣都是由Servlet容器管理,在第一次訪問時創建,在容器關閉時銷毀。
顯而易見的,DispatcherServler 實例在Servlet容器中,而不是Spring自己的IoC容器中。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM