Java Spring实际上是如何引导的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21714290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is Spring actually bootstrap?
提问by user3163426
- Does anybody know how Spring is actually bootstraps?
- Which instances created and by whom?
- I really want to know who creates instances of WebApplicationContext and ContextLoader. Is it work of Tomcat?
- 有谁知道 Spring 实际上是如何引导的?
- 哪些实例由谁创建?
- 我真的很想知道谁创建了 WebApplicationContext 和 ContextLoader 的实例。它是Tomcat的工作吗?
采纳答案by Pavel Horal
Servlet context listener (web.xml) approach
Servlet 上下文侦听器 (web.xml) 方法
- 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
creates newWebApplicationContext
with application context XML configuration.- Your ROOT context beans are registered and instantiated by
BeanFactory
inside the application context.
DispatcherServlet
is being instantiated by servlet container.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.
- 用户正在部署 Web 应用程序 WAR 。
- Servlet 容器(Tomcat) 读取
web.xml
. - Servlet 上下文侦听
ContextLoaderListener
器正在被 Servlet 容器实例化(如果定义为<listener>
在 内部web.xml
)。ContextLoaderListener
WebApplicationContext
使用应用程序上下文 XML 配置创建新的。- 您的 ROOT 上下文 bean
BeanFactory
在应用程序上下文中注册和实例化。
DispatcherServlet
正在被 servlet 容器实例化。DispatcherServlet
使用 ROOT 上下文作为其父级创建自己的WebApplicationContext
(WEB-INF/{servletName}-servlet.xml
默认情况下)。- 您的 servlet bean
BeanFactory
在应用程序上下文中注册和实例化。 DispatcherServlet
注册一些默认 bean,以防您自己不提供它们。
Servlet container initializer (non web.xml) approach
Servlet 容器初始值设定项(非 web.xml)方法
This one is possible with Servlet 3 features.
这可以通过 Servlet 3 功能实现。
- A web application WAR is being deployed by user.
- Servlet containersearches for classes implementing
ServletContainerInitializer
via Java'sServiceLoader
. - Spring's
SpringServletContainerInitializer
is found and instantiated by servlet container. - Spring's initializer readsweb application's class-path and searches for
WebApplicationInitializer
implementations. - Your
WebApplicationInitializer
is found (btw. check its JavaDoc!!!) and instantiated bySpringServletContainerInitializer
.- 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.
- Your
- Servlet containerfinishes the web application initialization and instantiates components which were registered by their class in previous steps (nonein my example).
- 用户正在部署 Web 应用程序 WAR 。
- Servlet 容器搜索
ServletContainerInitializer
通过 Java 的ServiceLoader
. - Spring
SpringServletContainerInitializer
是由 servlet 容器找到并实例化的。 - Spring 的初始化程序读取Web 应用程序的类路径并搜索
WebApplicationInitializer
实现。 - 你
WebApplicationInitializer
被找到了(顺便说一句。检查它的 JavaDoc!!!)并通过SpringServletContainerInitializer
.- 您
WebApplicationInitializer
WebApplicationContext
使用 XML 或@Configuration
基于配置创建新的 ROOT 。 - 您
WebApplicationInitializer
WebApplicationContext
使用 XML 或@Configuration
基于配置创建新的 servlet 。 - 您
WebApplicationInitializer
DispatcherServlet
在上一步的上下文中创建和注册新的。
- 您
- Servlet 容器完成 Web 应用程序初始化并实例化在前面步骤中由它们的类注册的组件(在我的示例中没有)。
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).
基于 Java 的方法更加灵活。您可以将上下文创建留给servlet 容器DispatcherServlet
,甚至可以将其整个实例化留给DispatcherServlet
servlet 容器(只需注册 servletDispatcherServlet.class
而不是其实例)。
回答by JB Nizet
See http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/htmlsingle/#context-create.
请参阅http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/htmlsingle/#context-create。
The principle is to declare a ServletContextListener in the standard webapp descriptor (web.xml). Such a listener is indeed instantiated by the container and is called when the application is initialized and when it's destroyed.
原理是在标准的webapp描述符(web.xml)中声明一个ServletContextListener。这样的侦听器确实由容器实例化,并在应用程序初始化和销毁时调用。
Spring provides such a ServletContextListener: ContextLoaderListenerwhich, as its name indicates, loads a Spring context when the webapp is initialized.
Spring 提供了这样一个 ServletContextListener:ContextLoaderListener,顾名思义,它在 webapp 初始化时加载 Spring 上下文。