Spring mvc零配置啟動(dòng)
- 運(yùn)用spi技術(shù)加載spring-web.jar 下面的META-INF/services/java.servlet.ServletContainerInitializer文件
- ServletContainerInitializer類中的@HandlerTypes注解會(huì)加載所有的實(shí)現(xiàn)了WebApplicationInitializer接口的類
- 所有被加載的類,會(huì)被放到onStartup方法的參數(shù)中,
- onStartup方法會(huì)實(shí)例化所有實(shí)現(xiàn)了WebApplicationInitializer接口的類,并調(diào)用他們的onStartup方法
- 在onStartup方法中會(huì)調(diào)用父類的onStartup方法,創(chuàng)建servletListener用來初始化Spring容器
- 在onStartup方法中還會(huì)創(chuàng)建DispatcherServlet對(duì)象,DispatcherServlet對(duì)象的init方法會(huì)加載Spring mvc容器,被設(shè)置Spring容器為它的父容器
- HandlerMapping、HandlerAdapter、ViewResolver實(shí)例的實(shí)例化,是借助@EnableWebmMvc和@Bean實(shí)現(xiàn)的的,在@Bean中會(huì)有鉤子方法,會(huì)調(diào)用父類的方法,而父類會(huì)通過@Autowired注解將所有實(shí)現(xiàn)了WebMvcConfigurer的實(shí)例加入到類屬性中,方便用戶自定義HandlerMapping、HandlerAdapter、ViewResolver
一個(gè)接口調(diào)用的流程
- servlet的service方法
- 調(diào)用JDK的super.service方法
- 區(qū)分請(qǐng)求類型,回調(diào)到spring-mvc包中的doGet/doPost/doPut/doDelete方法
- 都會(huì)統(tǒng)一調(diào)用processRequest方法
- 在processRequest中會(huì)調(diào)用doService方法
- 在doService方法中會(huì)調(diào)用doDispatch方法