smw*_*dia 11 java spring tomcat servlets servlet-3.0
据我所知,基于Spring的Web应用程序初始化如下:
第1步:Servlet container (e.g. Tomcat)找到实现ServletContainerInitializer,即SpringServletContainerInitializer.
第2步:SpringServletContainerInitializer创建DispatcherServlet和ContextLoaderListener
第3步:DispatcherServlet创建servlet application context.而ContextLoaderListener创建root application context.
第1步由Servlet 3.0规范定义.第2,3步完全由Spring定义.
我能看到理性投入的web豆类servlet上下文和non-web豆根上下文.但是,为什么我们要创建这些情境2 不同的地方,也就是DispatcherServlet和ContextLoaderListener?
如果所有我们想要的只是准备一切必要的,为什么不直接创造两种情况下ContextLoaderListener,因为它可以被看作是main()整个Web应用程序的方法.我认为这是更多的逻辑和当前的方法只会使事情复杂化.
根据@ Shailendra的回复,我画了这个:
我的理解是,Spring引入了application context概念并将它们存储在Servlet Context.Servlet Context是java servlet technolgoy引入的概念.
我想DispatcherServlet实现应该有一个成员变量来保存key它servlet application context的servlet context.所以它可以访问它自己的上下文.也许关键是servlet名称.
而root application context应该有一个众所周知的键,以便每个人都可以访问它.
在著名的关键root application context是这样的:
(中org.springframework.web.context.WebApplicationContext)
String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
Run Code Online (Sandbox Code Playgroud)
将DispatcherServlet确实有它的参考WebApplicationContext.它继承了以下memeber FrameworkServlet:
/** WebApplicationContext for this servlet */
private WebApplicationContext webApplicationContext;
Run Code Online (Sandbox Code Playgroud)
和
public FrameworkServlet(WebApplicationContext webApplicationContext) {
this.webApplicationContext = webApplicationContext;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3231 次 |
| 最近记录: |