Spring实际上是如何引导的? [英] How is Spring actually bootstrap?

查看:48
本文介绍了Spring实际上是如何引导的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 有人知道 Spring 实际上是如何引导的吗?
  2. 哪些实例由谁创建?
  3. 我真的很想知道谁创建了 WebApplicationContext 和 ContextLoader 的实例.它是Tomcat的工作吗?

推荐答案

Servlet 上下文监听器 (web.xml) 方法

  1. 用户正在部署一个 Web 应用程序 WAR.
  2. Servlet 容器(Tomcat)读取web.xml.
  3. Servlet 上下文监听器 ContextLoaderListener 正在被实例化(如果在 web.xml 中定义为 )通过 servlet 容器.
  1. A web application WAR is being deployed by user.
  2. Servlet container (Tomcat) reads web.xml.
  3. Servlet context listener ContextLoaderListener is being instantiated (if defined as <listener> inside the web.xml) by servlet container.
  1. ContextLoaderListener 创建新的 WebApplicationContext 带有应用程序上下文 XML 配置.
  2. 您的 ROOT 上下文 bean 由应用程序上下文内的 BeanFactory 注册和实例化.

  • DispatcherServlet 正在被servlet容器实例化.

    1. DispatcherServlet 创建自己的 WebApplicationContext(WEB-INF/{servletName}-servlet.xml 默认情况下) 以 ROOT 上下文作为其父级.
    2. 您的 servlet bean 由 BeanFactory 在应用程序上下文中.
    3. DispatcherServlet 注册一些默认 bean,以防您自己不提供它们.
    1. DispatcherServlet creates its own WebApplicationContext (WEB-INF/{servletName}-servlet.xml by default) with the ROOT context as its parent.
    2. Your servlet beans are registered and instantiated by BeanFactory inside the application context.
    3. 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.

    1. 用户正在部署一个 Web 应用程序 WAR.
    2. Servlet 容器 搜索实现 ServletContainerInitializer 通过 Java 的 ServiceLoader.
    3. Spring 的 SpringServletContainerInitializer被servlet容器发现并实例化.
    4. Spring 的初始化程序读取 Web 应用程序的类路径并搜索 WebApplicationInitializer 实现.
    5. 您的 WebApplicationInitializer 已找到(顺便说一下.检查其 JavaDoc!!!)并通过 SpringServletContainerInitializer实例化.
    1. A web application WAR is being deployed by user.
    2. Servlet container searches for classes implementing ServletContainerInitializer via Java's ServiceLoader.
    3. Spring's SpringServletContainerInitializer is found and instantiated by servlet container.
    4. Spring's initializer reads web application's class-path and searches for WebApplicationInitializer implementations.
    5. Your WebApplicationInitializer is found (btw. check its JavaDoc!!!) and instantiated by SpringServletContainerInitializer.
    1. 您的 WebApplicationInitializer 创建新的 ROOT WebApplicationContext 带有基于 XML 或 @Configuration 的配置.
    2. 您的 WebApplicationInitializer 创建新的 servlet WebApplicationContext 带有基于 XML 或 @Configuration 的配置.
    3. 您的 WebApplicationInitializer 使用上一步的上下文创建并注册新的 DispatcherServlet.
    1. Your WebApplicationInitializer creates new ROOT WebApplicationContext with XML or @Configuration based configuration.
    2. Your WebApplicationInitializer creates new servlet WebApplicationContext with XML or @Configuration based configuration.
    3. Your WebApplicationInitializer creates and registers new DispatcherServlet with the context from previous step.

  • Servlet 容器 完成 Web 应用程序初始化并实例化在前面步骤中由它们的类注册的组件(在我的示例中为none).
  • Servlet container finishes the web application initialization and instantiates components which were registered by their class in previous steps (none in my example).
  • 基于 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆