17.SpringMVC源码分析
1.服务器启动时的源码分析
寻找
init()
DispatcherServlet
类中没有init()
,在它的父类中寻找init()
FrameworkServlet
类中没有init()
,在它的父类中寻找init()
HttpServletBean.init()
HttpServletBean.init()
–>HttpServletBean.initServletBean()
,但是HttpServletBean.initServletBean()
方法体为空,找它的子类对initServletBean()
的实现FrameworkServlet.initServletBean()
–>FrameworkServlet.initWebApplicationContext()
FrameworkServlet.initWebApplicationContext()
–>FrameworkServlet.onRefresh()
,但是FrameworkServlet.onRefresh()
方法体为空,找它的子类对onRefresh()
的实现DispatcherServlet.onRefresh()
–>DispatcherServlet.initStrategies()
DispatcherServlet.initStrategies()
调用了initHandlerMappings()
,initHandlerAdapters()
,initViewResolvers()
等方法进行了初始化
DispatcherServlet.java
1 | public class DispatcherServlet extends FrameworkServlet { |
FrameworkServlet.java
1 | public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware { |
HttpServletBean.java
1 | public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware { |
2.当接收用户请求时的源码分析
寻找
service()
DispatcherServlet
类中没有service()
,在它的父类中寻找service()
FrameworkServlet.service()
FrameworkServlet.service()
–>FrameworkServlet.processRequest()
FrameworkServlet.processRequest()
–>FrameworkServlet.doService()
,但是FrameworkServlet.doService()
方法是一个抽象方法,找它的子类对doService()
的实现DispatcherServlet.doService()
–>DispatcherServlet.doDispatch()
DispatcherServlet.doDispatch()
–>org.springframework.web.servlet.HandlerAdapter.handle()
和DispatcherServlet.processDispatchResult()
DispatcherServlet.processDispatchResult()
–>DispatcherServlet.render()
DispatcherServlet.render()
–>org.springframework.web.servlet.View.render()
FrameworkServlet.java
1 |
|
DispatcherServlet.java
1 | public class DispatcherServlet extends FrameworkServlet { |