本文介绍了Spring实际上是如何引导的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 有人知道 Spring 实际上是如何引导的吗?
- 哪些实例由谁创建?
- 我真的很想知道谁创建了 WebApplicationContext 和 ContextLoader 的实例.它是Tomcat的工作吗?
推荐答案
Servlet 上下文监听器 (web.xml) 方法
- 用户正在部署一个 Web 应用程序 WAR.
- Servlet 容器(Tomcat)读取
web.xml
. - Servlet 上下文监听器
ContextLoaderListener
正在被实例化(如果在web.xml
中定义为)通过 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
注册和实例化.
DispatcherServlet
正在被servlet容器实例化.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
实现. - 您的
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
创建新的 servletWebApplicationContext
带有基于 XML 或@Configuration
的配置. - 您的
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实际上是如何引导的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!