Spring究竟是如何引导的? [英] How is Spring actually bootstrap?

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

问题描述


  1. 有人知道Spring实际上是如何引导的吗?

  2. 创建了哪些实例以及由谁创建?

  3. 我真的想知道是谁创建了WebApplicationContext和ContextLoader的实例。这是Tomcat的工作吗?


推荐答案

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



Servlet context listener (web.xml) approach


  1. 用户正在部署Web应用程序WAR。

  2. Servlet容器(Tomcat)读取 web.xml

  3. Servlet上下文侦听器 ContextLoaderListener 正在实例化(如果在 web.xml 中定义为< listener> 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 注册并实例化应用程序上下文。

  1. ContextLoaderListener creates new WebApplicationContext with application context XML configuration.
  2. Your ROOT context beans are registered and instantiated by BeanFactory inside the application context.


  • DispatcherServlet 由servlet容器实例化

  • DispatcherServlet is being instantiated by servlet container.


    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. 您的<$找到c $ c> 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 @Configuration 进行配置的noreferrer> WebApplicationContext

    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天全站免登陆