Skip to main content
Advertisement

2.3 Starters & Auto-Configuration

Let's dive into the principles behind the 'Starters' that we easily added with a few clicks in the previous chapter, and the magic of 'Auto-Configuration', which started the Tomcat web server without writing a single line of code. These two concepts are the alpha and omega of Spring Boot.

1. Spring Boot Starters

In the past, to launch a web application, developers had to individually search for and copy dozens of libraries like spring-webmvc, jackson-databind, and tomcat-embed-core into build.gradle (or pom.xml), painstakingly matching their versions. If a version was even slightly mismatched, terrible, untraceable compilation conflict errors would occur.

The Starter Solution

To achieve a specific goal (web development, DB connection, security, etc.), Spring Boot provides a packaged set of libraries (a bundle of dependencies) that are best used together. This is what a Starter is.

If you open the build.gradle file, you will see a code snippet like this:

dependencies {
// Only one line was added, but...
implementation 'org.springframework.boot:spring-boot-starter-web'
}

The moment this single line is added, behind the scenes, over 30 sub-essential libraries, including the stability-verified embedded Tomcat server library, JSON processing libraries, and REST API processing core modules, are bundled together and automatically downloaded by Spring Boot.

2. The Magic of Auto-Configuration

"I only downloaded the libraries, but how could a Tomcat server spin up on port 8080 right away without a single line of code?"

The secret is hidden within a single annotation attached to the main class automatically generated when the project was created: @SpringBootApplication.

Internal Structure of @SpringBootApplication

Although it has just one name, if you press Ctrl (or Cmd on Mac) and click on the annotation, you will find that it internally embraces three core features.

  1. @SpringBootConfiguration: Indicates that this is the main, primary configuration file for Spring Boot.
  2. @ComponentScan: It takes the package to which this class belongs as the topmost root and rigorously scans to find and register all beans (@Controller, @Service, etc.) located below it into memory.
  3. @EnableAutoConfiguration: This is the core magic trick. ** It glances at the Classpath to verify whether Tomcat or Web-related library files, brought in earlier by the 'Starter', physically exist.**The moment it discovers them, it immediately figures out, "Ah, this developer intends to build a web server!" and automatically executes dozens of lines of invisible, initial Java setup code in the background to launch Tomcat exactly on port 8080 seamlessly.

Summary: Spring Boot's Two-Step Combo

  1. "I want to make a Web Server"-> The developer strictly adds a single dependency line spring-boot-starter-web locally. (Ingredients Ready).
  2. "Okay, I will set up Tomcat and the web environment for you"-> The Auto-Configuration feature of @SpringBootApplication automatically identifies the ingredients actively reliably natively successfully smoothly seamlessly flawlessly cleanly expertly expertly skillfully completely functionally expertly comfortably directly safely dynamically and perfectly cooks the meal cleanly and sets the table thoroughly efficiently properly logically cleanly optimally flawlessly effectively.
Advertisement