微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringMvc-DispatchServlet初始化

WebApplicationContext rootContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext());
ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
业务容器

request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE, getWebApplicationContext());
webApplicationContext
web容器

HttpServletBean

public final void init() throws servletexception {...}
第一次调用serverlet时触发
initServletBean();

FrameworkServlet

protected final void initServletBean() throws servletexception {...}
重写了HttpServletBean的initServletBean()
初始化web容器

this.webApplicationContext = initWebApplicationContext();

protected WebApplicationContext initWebApplicationContext() {...}

WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
获取父容器(业务容器),springboot在启动时就创建了,并设置到serverlet中

onRefresh(wac); 调用的是dispatchServlet重写的onRefresh(wac)
初始化springmvc各大组件

dispatchServlet

protected void onRefresh(ApplicationContext context) {...}
initStrategies(context);

protected void initStrategies(ApplicationContext context) {...}initMultipartResolver(context);
initLocaleResolver(context);
initThemeResolver(context);
initHandlerMappings(context);
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);

-initHandlerMappings(context){...};
所谓初始化,不过是将ioc容器中handlerMapping,根据handlerMapping.class,从ioc容器,全部获取出来,
然后装到dispatchServlet的List handlerMappings中。

===========
在SpringBoot的ServletWebServerApplicationContext,貌似没有父子容器,在service层也可以调Controller的方法。controller是一个@Component,所以会被Spring注册到ioc容器。然后初始化HandlerMapping即Controller到ioc容器。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐