How is Spring actually bootstrap?

前端 未结 2 702
栀梦
栀梦 2020-12-04 12:52
  1. Does anybody know how Spring is actually bootstraps?
  2. Which instances created and by whom?
  3. I really want to know who creates instances of WebApplica
相关标签:
2条回答
  • 2020-12-04 13:18

    See 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.

    Spring provides such a ServletContextListener: ContextLoaderListener which, as its name indicates, loads a Spring context when the webapp is initialized.

    0 讨论(0)
  • 2020-12-04 13:20

    Servlet context listener (web.xml) approach

    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 creates new WebApplicationContext with application context XML configuration.
      2. Your ROOT context beans are registered and instantiated by BeanFactory inside the application context.
    4. DispatcherServlet is being instantiated by servlet container.
      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 container initializer (non web.xml) approach

    This one is possible with Servlet 3 features.

    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. 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.
    6. Servlet container finishes the web application initialization and instantiates components which were registered by their class in previous steps (none in my example).

    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).

    0 讨论(0)
提交回复
热议问题