DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Trending

  • Designing AI Multi-Agent Systems in Java
  • Data Lake vs. Warehouse vs. Lakehouse vs. Mart: Choosing the Right Architecture for Your Business
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot Under the Hood

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.

By 
Ganesh Sahu user avatar
Ganesh Sahu
·
Apr. 06, 18 · Tutorial
Likes (34)
Comment
Save
Tweet
Share
56.8K Views

Join the DZone community and get the full member experience.

Join For Free

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)

Spring Framework Spring Boot application

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

Opinions expressed by DZone contributors are their own.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!