springboot中springmvc的初始过程
首先,在ServletWebServerApplicationContext的refresh的方法中,会调用createWebServer()方法来创建webServer org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh。
看一下createWebServer方法,首先会判断servletContext是否为空,如果为空,则会先获取ServletWebServerFactory,再进行初始化
private void createWebServer() {WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory();
this.webServer = factory.getWebServer(getSelfInitializer());
}
else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context", ex);
}
}
initPropertySources();
}
在selfInitialize中,会进行ServletContextInitializer的初始化,在getServletContextInitializerBeans中,在此方法中,通过调用new ServletContextInitializerBeans获取到一个List<ServletContextInitializer>数组,其中就包括了我们熟知的DispatchServlet和定义的Filter等等。
private void selfInitialize(ServletContext servletContext) throws ServletException {prepareWebApplicationContext(servletContext);
registerApplicationScope(servletContext);
WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(), servletContext);
for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
beans.onStartup(servletContext);
}
}
beans.onStartup过程中,进行了servlet和filter的注册,这个过程在org.springframework.boot.web.servlet.DynamicRegistrationBean#register方法中进行,会将servlet加入到servletContext中,或者将filter加入servletContext中去。
以上是 springboot中springmvc的初始过程 的全部内容, 来源链接: utcz.com/z/511718.html