What is Spring Boot?
Spring Boot is an opinionated framework built on top of the Spring Framework. It helps developers create production-ready Java applications quickly by choosing sensible defaults and reducing the amount of configuration they must write.
Opinionated does not mean that Spring Boot removes your choices. It means Spring Boot provides a good default first, while still allowing you to change the configuration when your application needs something different.
Why was Spring Boot created?
Before Spring Boot, starting a Spring application often meant configuring many separate pieces manually:
web.xmland theDispatcherServlet- A servlet container such as Tomcat
- Maven or Gradle dependencies and compatible versions
- The Spring application context and bean configuration
- Property files and environment-specific settings
This setup could require hundreds of lines of configuration before the first business feature was written.
Minimal Spring Boot application
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
With this class and the right starter dependency, Spring Boot can start the application context and an embedded web server. You can run it like a normal Java program, without separately installing and deploying a WAR file to Tomcat.
Key advantages
| Advantage | What it means |
|---|---|
| Embedded Tomcat | The server can run inside your executable JAR. |
| Auto-configuration | Spring Boot configures common components when their dependencies are present. |
| Starter dependencies | One dependency brings a compatible group of libraries. |
| Less XML | Java configuration, annotations, and properties replace most manual XML setup. |
| Production features | Health checks, metrics, external configuration, and logging support are available. |
| Easy deployment | Build one JAR and run it with java -jar. |
Real-world analogy: opening a restaurant
Imagine opening a restaurant.
Spring Boot prepares the common infrastructure. You still decide what your application does, how its business rules work, and which features your users receive.
Spring vs Spring Boot
Spring is a large ecosystem for building Java applications. Traditionally, developers configured many objects and framework integrations by hand. Spring Boot adds conventions, starter dependencies, embedded servers, and automatic configuration to reduce that setup work.
Minimal application
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@SpringBootApplication combines component scanning,
auto-configuration, and a configuration class.
SpringApplication.run() creates the application context and
starts the embedded server when web dependencies are present.
Key ideas
- Convention over configuration: common choices work with little code.
- Starters: dependency bundles for web, data, security, and testing.
- Embedded server: run the app as a normal Java process or JAR.
- External configuration: change environment-specific values without recompiling.
Auto-Configuration
Auto-configuration is Spring Boot's way of choosing common beans for you. Boot checks the libraries on the classpath, your configuration properties, and the beans you already defined. It creates sensible defaults only when they are needed.
For example, when the web starter is present, Boot can configure an embedded server, Spring MVC infrastructure, JSON support, and a dispatcher servlet. If you define your own bean, Boot usually backs away so your explicit choice wins.
Including dependencies to activate auto-configuration
Auto-configuration is driven by the dependencies in your project. Adding a starter places the required libraries on the classpath, so Spring Boot can detect the feature and configure it.
<!-- Adds Spring MVC, Jackson, validation support, and embedded Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Adds JPA, Hibernate, transactions, and repository support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
After these dependencies are available, Boot checks their classes and creates the matching infrastructure. A dependency does not automatically create a useful application feature by itself; your controllers, entities, repositories, and properties still need to be provided.
Excluding an auto-configuration class
Sometimes a dependency is present but your application does not need the feature it normally activates. You can exclude the related auto-configuration class.
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The same exclusion can be configured without changing the Java class:
# application.properties
spring.autoconfigure.exclude=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
This is useful for a service that includes a database-related library transitively but does not connect to a database. Exclude only what you understand; removing required auto-configuration can cause missing beans or startup errors.
Excluding an individual dependency
Dependency exclusion is different from auto-configuration exclusion. A dependency exclusion prevents a library from being downloaded or used, while an auto-configuration exclusion keeps the library but prevents Boot from creating its default beans.
<!-- Keep Spring MVC, but remove embedded Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Replacing embedded Tomcat with Jetty
Spring Boot's web starter uses embedded Tomcat by default. To use Jetty instead, exclude the Tomcat starter and add the Jetty starter. Do not keep both servlet containers in the same application because the server configuration can become ambiguous.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Add the alternative embedded servlet container -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Your controllers and REST endpoints usually do not change because Spring MVC sits above the servlet container. The main difference is the embedded server that receives HTTP requests and starts the application.
Useful auto-configuration controls
| Requirement | Approach | Example |
|---|---|---|
| Add a feature | Add a starter or library | spring-boot-starter-data-jpa |
| Change a default value | Set a property | server.port=8081 |
| Replace a default bean | Define your own @Bean |
Custom ObjectMapper |
| Disable configuration |
Use exclude or
spring.autoconfigure.exclude
|
DataSourceAutoConfiguration |
| Change the web server | Exclude Tomcat and add Jetty | spring-boot-starter-jetty |
Starters
A Spring Boot Starter is a carefully selected group of dependencies for one application capability. Instead of finding and adding every library separately, you add one starter and Spring Boot brings in the commonly required libraries.
spring-boot-starter-webExample: adding the Web starter
To build REST APIs, add the Web starter to pom.xml:
spring-boot-starter-web