Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Atomist automates your software deliver experience. It's how modern teams deliver modern software.

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

Answer:

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?

Answer:

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?

Answer:

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

    this();

    register(annotatedClasses);

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

}

 

Question: Which servlet acts as a front controller?

Answer:

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?

Answer:

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

Include the following dependencies in Maven         P   

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>${tomcat.version}</version>
</dependency>


And write a class to bootstrap Tomcat:

Tomcat tomcat = new Tomcat();

tomcat.setPort(8080);

// Create context object and set it

tomcat.addContext ("/mycontext);

tomcat.start();

tomcat.getServer().await();

 

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)

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:
java ,spring boot ,context ,embedded servers ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}