Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Spring actually bootstrap?

  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 WebApplicationContext and ContextLoader. Is it work of Tomcat?
like image 817
user3163426 Avatar asked Feb 11 '14 22:02

user3163426


People also ask

How does bootstrap work in spring boot?

One of the ways to Bootstrapping a Spring Boot application is by using Spring Initializer. To do this, you will have to visit the Spring Initializer web page www.start.spring.io and choose your Build, Spring Boot Version and platform.

Is spring boot the same as bootstrap?

Bootstrap belongs to "Front-End Frameworks" category of the tech stack, while Spring Boot can be primarily classified under "Frameworks (Full Stack)".

What does bootstrapping mean in spring?

Bootstrapping is a process of initializing an application. Bootstrapping in spring boot application is done using Spring Initializer. You can Bootstrap your spring boot application by visiting www.start.spring.io.

What actually is spring boot?

Java Spring Boot (Spring Boot) is a tool that makes developing web application and microservices with Spring Framework faster and easier through three core capabilities: Autoconfiguration. An opinionated approach to configuration. The ability to create standalone applications.


2 Answers

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

like image 114
Pavel Horal Avatar answered Oct 11 '22 07:10

Pavel Horal


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.

like image 45
JB Nizet Avatar answered Oct 11 '22 08:10

JB Nizet