Spring究竟是如何引导的? [英] How is Spring actually bootstrap?
本文介绍了Spring究竟是如何引导的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 有人知道Spring实际上是如何引导的吗?
- 创建了哪些实例以及由谁创建?
- 我真的想知道是谁创建了WebApplicationContext和ContextLoader的实例。这是Tomcat的工作吗?
推荐答案
Servlet上下文监听器(web.xml)方法
Servlet context listener (web.xml) approach
- 用户正在部署Web应用程序WAR。
- Servlet容器(Tomcat)读取
web.xml
。 - Servlet上下文侦听器
ContextLoaderListener
正在实例化(如果在web.xml
中定义为< listener>
) servlet容器。
- A web application WAR is being deployed by user.
- Servlet container (Tomcat) reads
web.xml
. - Servlet context listener
ContextLoaderListener
is being instantiated (if defined as<listener>
inside theweb.xml
) by servlet container.
-
ContextLoaderListener
创建新的WebApplicationContext
使用应用程序上下文XML配置。 - 您的ROOT上下文bean由
BeanFactory
注册并实例化应用程序上下文。
ContextLoaderListener
creates newWebApplicationContext
with application context XML configuration.- Your ROOT context beans are registered and instantiated by
BeanFactory
inside the application context.
DispatcherServlet
由servlet容器实例化 。 DispatcherServlet
is being instantiated by servlet container.
-
DispatcherServlet
创建自己的WebApplicationContext
(默认情况下为WEB-INF / {servletName} -servlet.xml
)ROOT上下文为其父项。 - 您的servlet bean是由
BeanFactory
。 -
DispatcherServlet
会注册一些默认bean,以防您自己没有提供。
DispatcherServlet
creates its ownWebApplicationContext
(WEB-INF/{servletName}-servlet.xml
by default) with the ROOT context as its parent.- Your servlet beans are registered and instantiated by
BeanFactory
inside the application context. DispatcherServlet
registers some default beans in case you did not provide them yourself.
Servlet容器初始化程序(非web.xml)方法
这个是可以使用Servlet 3功能。
Servlet container initializer (non web.xml) approach
This one is possible with Servlet 3 features.
- 用户正在部署Web应用程序WAR。
- Servlet容器搜索实现
ServletContainerInitializer
来自Java的ServiceLoader
。 - Spring的
SpringServletContainerInitializer
由servlet容器找到并实例化。 - Spring的初始化程序读取 Web应用程序的类路径并搜索
WebApplicationInitializer
实施。 - 您的<$找到c $ c> WebApplicationInitializer (顺便说一句。 检查其JavaDoc !!! )并通过
SpringServletContainerInitializer
实例化。
- A web application WAR is being deployed by user.
- Servlet container searches for classes implementing
ServletContainerInitializer
via Java'sServiceLoader
. - Spring's
SpringServletContainerInitializer
is found and instantiated by servlet container. - Spring's initializer reads web application's class-path and searches for
WebApplicationInitializer
implementations. - Your
WebApplicationInitializer
is found (btw. check its JavaDoc!!!) and instantiated bySpringServletContainerInitializer
.
- 您的
WebApplicationInitializer
创建新的ROOTWebApplicationContext
使用基于XML或@Configuration
的配置。 - 您的
WebApplicationInitializer
创建新的servlet @Configuration 进行配置的noreferrer>WebApplicationContext
。 - 您的
WebApplicationInitializer
使用上一步中的上下文创建并注册新的DispatcherServlet
。
- Your
WebApplicationInitializer
creates new ROOTWebApplicationContext
with XML or@Configuration
based configuration. - Your
WebApplicationInitializer
creates new servletWebApplicationContext
with XML or@Configuration
based configuration. - Your
WebApplicationInitializer
creates and registers newDispatcherServlet
with the context from previous step.
基于Java的方法更加灵活。您可以将上下文创建保留到 DispatcherServlet
,甚至将整个实例化 DispatcherServlet
本身保存到servlet容器(只需注册servlet DispatcherServlet.class
而不是它的实例。
Java based approach is much more flexible. You can leave the context creation to DispatcherServlet
or even the whole instantiation of DispatcherServlet
itself to servlet container (just register servlet DispatcherServlet.class
instead of its instance).
这篇关于Spring究竟是如何引导的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文