Introduction to Spring Boot
Reading Time: 14 Minutes
Difficulty: Beginner
Topic Summaryโ
Spring Boot is the fastest way to build production-ready Java applications. It takes the powerful Spring Framework and wraps it with smart defaults and auto-configuration so you can go from zero to a running application in minutes โ with almost no setup. If Spring Framework is the engine, Spring Boot is the car with the ignition already hot.
What You'll Learnโ
- What Spring Boot is and why developers love it
- How auto-configuration works (the magic behind Spring Boot)
- What the embedded server (Tomcat) is and why it matters
- How to use Spring Initializr to generate a project
- The structure of a Spring Boot project
- What
@SpringBootApplicationdoes - How to write and run your first Spring Boot application
Prerequisitesโ
- Understanding of Spring Framework basics (IoC, DI)
- Basic Java knowledge (classes, annotations, methods)
- Java 17+ installed
- Maven or Gradle installed (Maven is used in examples)
Explanationโ
What is Spring Boot?โ
Spring Boot is an opinionated, convention-over-configuration framework built on top of Spring Framework. It removes the pain of setting up a Spring application by:
- Auto-configuring Spring and third-party libraries based on what's on your classpath
- Embedding a web server (Tomcat by default) directly into your JAR file
- Providing starter dependencies that bundle related libraries together
- Generating production-ready apps with no XML configuration required
Before Spring Boot, setting up a Spring web application required: configuring a DispatcherServlet in web.xml, setting up a Spring MVC context, configuring a view resolver, deploying a WAR file to an external Tomcat server... pages of XML. Spring Boot replaced all of that with a single annotation: @SpringBootApplication.
How Auto-Configuration Worksโ
Auto-configuration is the core magic of Spring Boot. Here's how it works:
- When your app starts, Spring Boot scans your classpath (the JARs you've added as dependencies)
- It checks which libraries are present (e.g., Is Tomcat on the classpath? Is H2 database present? Is Jackson present?)
- Based on what it finds, it automatically creates and configures the right beans
Example: If you add spring-boot-starter-web to your project:
- Spring Boot sees Tomcat on the classpath โ automatically sets up an embedded Tomcat server on port 8080
- It sees Jackson (JSON library) โ automatically configures JSON serialization/deserialization
- It creates a
DispatcherServletfor you
You can override any auto-configured bean by creating your own. Spring Boot always respects your explicit configuration over its defaults.
Auto-configuration classes live in spring-boot-autoconfigure.jar. The @EnableAutoConfiguration annotation (part of @SpringBootApplication) triggers this process.
Embedded Server โ No More WAR Files!โ
Traditionally, you would:
- Write your app
- Package it as a WAR file
- Deploy the WAR to an external Tomcat server
- Start the Tomcat server
- Navigate to your app's URL
With Spring Boot:
- Write your app
- Run
java -jar myapp.jarโ That's it!
The Tomcat server is embedded inside the JAR file. This means:
- Your app is fully self-contained
- Easy to deploy anywhere Java runs
- Great for Docker containers and microservices
- No more "it works on my Tomcat but not yours" problems
You can switch from Tomcat to Jetty or Undertow by simply swapping dependencies.
Spring Initializr โ Your Project Generatorโ
Spring Initializr at https://start.spring.io is a web tool that generates a Spring Boot project for you.
Steps:
- Go to start.spring.io
- Choose: Project (Maven), Language (Java), Spring Boot version (3.x)
- Fill in: Group (
com.example), Artifact (myapp), Name, Description - Select Java version (17 or 21)
- Add dependencies (e.g., Spring Web, Spring Data JPA, MySQL Driver)
- Click Generate โ download a ZIP file
- Extract and open in IntelliJ IDEA or Eclipse
Your IDE also has built-in Spring Initializr support (IntelliJ: File โ New โ Spring Initializr).
Spring Boot Project Structureโ
After generating a project, you get this structure:
myapp/
โโโ src/
โ โโโ main/
โ โ โโโ java/
โ โ โ โโโ com/example/myapp/
โ โ โ โโโ MyappApplication.java โ Main class
โ โ โโโ resources/
โ โ โโโ application.properties โ Configuration file
โ โ โโโ static/ โ Static files (CSS, JS, images)
โ โ โโโ templates/ โ HTML templates (if using Thymeleaf)
โ โโโ test/
โ โโโ java/
โ โโโ com/example/myapp/
โ โโโ MyappApplicationTests.java
โโโ pom.xml โ Maven build file
โโโ HELP.md
Key files:
MyappApplication.javaโ Entry point withmain()method and@SpringBootApplicationapplication.propertiesโ Configure port, database, logging, etc.pom.xmlโ Dependencies and build configuration
The @SpringBootApplication Annotationโ
This single annotation on your main class does three things:
@SpringBootApplication
// is equivalent to:
@SpringBootConfiguration // Marks this as a configuration class (@Configuration)
@EnableAutoConfiguration // Enables Spring Boot's auto-configuration
@ComponentScan // Scans for @Component, @Service, @Repository, @Controller in this package and sub-packages
It's the master switch that starts the entire Spring Boot magic.
Key application.properties Settingsโ
# Server port (default is 8080)
server.port=8080
# Application name
spring.application.name=myapp
# Database
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
# JPA / Hibernate
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
# Logging level
logging.level.org.springframework=INFO
Spring Boot Starter Dependenciesโ
Starters are pre-packaged sets of dependencies. Instead of adding 10 separate JARs, you add one starter:
| Starter | What It Includes |
|---|---|
spring-boot-starter-web | Spring MVC, Tomcat, Jackson |
spring-boot-starter-data-jpa | Spring Data JPA, Hibernate |
spring-boot-starter-security | Spring Security |
spring-boot-starter-test | JUnit, Mockito, AssertJ |
spring-boot-starter-thymeleaf | Thymeleaf template engine |
spring-boot-starter-actuator | Health checks, metrics, monitoring |
Real-World Analogyโ
Building a web app with old Spring was like buying a car in parts โ you get the engine, wheels, body, electronics separately and assemble them yourself. Exhausting!
Spring Boot is like buying a fully assembled car from a factory. You get in, turn the key (main() method), and drive. All the parts are already configured to work together. If you want to customize โ swap the stereo (change JSON library), change the seats (switch from Tomcat to Jetty) โ you can, but the defaults are great.
Spring Initializr is the car configurator on the website โ pick your options, click "generate," and the factory ships you the ready-to-drive car.
Code Exampleโ
pom.xml (Spring Boot Maven Setup)โ
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Spring Boot Parent โ provides dependency management -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>My first Spring Boot app</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- Web starter: Spring MVC + embedded Tomcat + Jackson -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Test starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Plugin to create executable JAR -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Main Application Classโ
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// This single annotation does: @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
// Boots up the entire Spring application
SpringApplication.run(DemoApplication.class, args);
System.out.println("๐ Spring Boot Application Started!");
}
}
A Simple REST Controllerโ
package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController // Marks this as a controller that returns data (not HTML views)
public class HelloController {
@GetMapping("/hello") // Maps HTTP GET /hello to this method
public String sayHello() {
return "Hello from Spring Boot! ๐";
}
@GetMapping("/")
public String home() {
return "Welcome to my first Spring Boot app!";
}
}
Run the Applicationโ
# From project root directory
mvn spring-boot:run
# OR build and run the JAR
mvn clean package
java -jar target/demo-0.0.1-SNAPSHOT.jar
Output (Console)โ
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.2.0)
Started DemoApplication in 2.456 seconds (JVM running for 3.012)
๐ Spring Boot Application Started!
Test in Browserโ
Visit: http://localhost:8080/hello
Hello from Spring Boot! ๐
Common Mistakesโ
- โ Mistake: Placing the main class in the wrong package (not the root package) โ โ
Fix: Keep
@SpringBootApplicationin the root package โ@ComponentScanscans sub-packages from there - โ Mistake: Forgetting to add
spring-boot-starter-webfor web apps โ โ Fix: Always check that the right starters are in yourpom.xml - โ Mistake: Hardcoding passwords and database URLs in
application.propertiesโ โ Fix: Use environment variables orapplication-{profile}.propertiesfor sensitive config - โ Mistake: Expecting Spring Boot to configure things it can't detect โ โ Fix: If you want something auto-configured, the dependency must be on the classpath
Best Practicesโ
- Always use the latest stable Spring Boot version (check spring.io)
- Use Spring Initializr to generate projects โ never set up from scratch manually
- Organize code in packages:
controller,service,repository,model - Store all configuration in
application.propertiesorapplication.ymlโ not hardcoded in Java - Use profiles (
dev,test,prod) to separate configurations per environment - Use
spring-boot-starter-actuatorin production for health checks
Interview Questionsโ
Q: What is Spring Boot and how is it different from Spring Framework?
A: Spring Boot is an opinionated framework built on top of Spring Framework that adds auto-configuration, embedded server support (Tomcat by default), and starter dependencies. While Spring Framework requires extensive manual configuration, Spring Boot follows "convention over configuration" โ it auto-configures your application based on what's on the classpath, letting you get started in minutes.
Q: What does @SpringBootApplication do?
A: It's a composed annotation that combines three annotations: @SpringBootConfiguration (marks the class as a configuration class), @EnableAutoConfiguration (enables Spring Boot's auto-configuration mechanism), and @ComponentScan (scans the current package and sub-packages for Spring components). It's the entry point annotation for any Spring Boot application.
Q: What is auto-configuration in Spring Boot?
A: Auto-configuration is Spring Boot's ability to automatically configure Spring beans based on what libraries are present on the classpath. For example, if spring-boot-starter-web is in the classpath, Spring Boot automatically configures an embedded Tomcat server, a DispatcherServlet, and JSON support. You can override any auto-configured bean with your own explicit configuration.
Q: What are Spring Boot Starters?
A: Starters are pre-packaged sets of Maven/Gradle dependencies that work well together. For example, spring-boot-starter-web pulls in Spring MVC, embedded Tomcat, and Jackson โ all in one dependency. They follow the naming convention spring-boot-starter-* and eliminate the need to figure out which libraries and versions to use together.
Q: What is the embedded server in Spring Boot?
A: Spring Boot embeds a web server (Tomcat by default, or Jetty/Undertow) directly inside the application JAR file. This means you don't need to install or configure an external server โ you just run java -jar app.jar and the server starts automatically. This makes deployment simple and makes Spring Boot ideal for microservices.
Quick Revisionโ
โ Spring Boot = Spring Framework + Auto-configuration + Embedded Tomcat + Zero XML config
โ @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
โ Auto-configuration detects classpath and sets up beans automatically
โ Spring Initializr (start.spring.io) generates project scaffolding in seconds
โ Starters bundle related dependencies together (e.g., spring-boot-starter-web)
โ Run with mvn spring-boot:run or java -jar โ no external server needed
Related Topicsโ
- Spring Framework (IoC, DI)
- REST API with Spring Boot
- Spring Data JPA
- Spring Security Basics
Next Lessonโ
Lesson 3 โ Building REST APIs with Spring Boot