Spring Boot Under the Hood

DZone 's Guide to

Spring Boot Under the Hood

It's always good to make sure you understand how your favorite tools work internally. Here, we run through a few basic concepts in Spring Boot and see how the magic happens.

· Java Zone ·
Free Resource

Spring Boot has created such a hype and I feel understanding the basics is necessary for any seasoned developer.

When we do a SpringApplication.run(ApplicationBootClass.class), it starts the application, and we have a working application serving our needs (if coded properly, of course).

However, I was curious to know about how things work under the hood. There are many questions, like:

  • How can an embedded Tomcat run with such a simple configuration?

  • Who injects the WAR/JAR to Tomcat and how does it start?

These were some of the questions that led me to dig into the source material and get a clearer understanding of Spring Boot.

Let's try understanding the answers to some of these questions.

Question: How are Beans getting created (BeanFactory or ApplicationContext)?


When SpringApplication.run() command is invoked, the Application Context is created by calling the method below:

public ConfigurableApplicationContext run(String... args) { 

    // Create, load, refresh, and run the ApplicationContext
    context = createApplicationContext();            

    return context ; // handle to the context object for the developer



Question: What exactly is the type of this context?


The createApplicationContext method checks if it is a web or standalone application based on the type it creates for the context. I was creating a REST-based controller for which a context of type AnnotationConfigEmbeddedWebApplicationContext was initialized. In the case of a standalone application, AnnotationConfigApplicationContext will be initialized. 

Question: How are the beans created once the context is initialized?


When the constructor of the context is invoked, it will register the annotated class beans with the context. That's why no XML configurations are required. All your @Repository, @Component, @Service, and Controller beans will be registered and the context is returned. The following lines of code are executed for context initialization and bean creation for a web application.

public AnnotationConfigEmbeddedWebApplicationContext(Class<?>... annotatedClasses) {



    refresh(); // Refreshing org.springframework.boot.context.embedded. This log appears in the console



Question: Which servlet acts as a front controller?


No prizes for guessing that: DispatcherServlet.

The AnnotationConfigEmbeddedWebApplicationContext class extends the EmbeddedWebApplicationContext, which registers the dispatcher servlet.

public static final String DISPATCHER_SERVLET_NAME = ServletContextInitializerBeans.DISPATCHER_SERVLET_NAME;


Question: What about the embedded Tomcat?


Normally, starting an embedded Tomcat is as easy as instantiating the Tomcat class.

Include the following dependencies in Maven         P   


And write a class to bootstrap Tomcat:

Tomcat tomcat = new Tomcat();


// Create context object and set it

tomcat.addContext ("/mycontext);




So with regards to Spring Boot. the EmbeddedWebApplicationContext creates an instance of org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer and adds the context. 

TomcatEmbeddedServletContainer class has Tomcat as an instance variable.

Check the selfInitialize() method and prepareEmbeddedWebApplicationContext of the EmbeddedWebApplicationContext class:

prepareEmbeddedWebApplicationContext() {

    servletContext.log("Initializing Spring embedded WebApplicationContext"); // these logs are printed in your STS console.

    logger.info("Root WebApplicationContext: initialization completed in " );  // these logs are printed in your STS console.


More questions and answers to follow. Stay Tuned. 

Source code reference (www.grepcode.com)

java ,spring boot ,context ,embedded servers ,tutorial

Published at DZone with permission of Ganesh Sahu . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}