Sub topic 2
What Spring Boot Is and the Problems It Solves
This chapter explains what Spring Boot is, what work it removes, and where its limits are. It does not walk through project creation yet. Later chapters cover that.
It assumes you can read basic Java, have used Maven or Gradle to build a project, know roughly how HTTP requests and responses work, and have a JDK and a terminal. It also assumes you have seen dependency injection (DI) before. In DI, an object receives the collaborators it needs from outside instead of constructing them itself.
Where Spring Boot Sits
The Spring Framework is a large library built around an application context. The context is a container that creates objects called beans, wires them together by DI, and manages their lifecycle. Spring MVC, data access, transactions, and security all build on that container.
Spring Boot is not a replacement for the Spring Framework. It is a layer on top of it. Your code still uses Spring annotations, beans, and DI. Boot decides which beans to create for you and packages the result so it runs with one command.
The Problems Boot Addresses
Before Boot, a Spring web application needed a lot of setup before it did anything useful. The setup fell into four areas.
- Dependency selection. You had to pick compatible versions of Spring MVC, Jackson, a validation library, a logging backend, and a test library. A mismatch showed up at runtime as a
NoSuchMethodErroror aClassNotFoundException. - Bean configuration. You declared a
DispatcherServlet, a view or message converter setup, aDataSource, a transaction manager, and more. Most of these declarations were nearly identical across projects. - Deployment. You built a WAR file and deployed it to a separately installed servlet container such as Tomcat. Versions and settings of that container became a second thing to keep in sync.
- Operations defaults. Health checks, metrics, and externalized configuration were each wired up by hand.
Boot answers each area with a specific mechanism:
| Problem | Boot mechanism | Effect |
|---|---|---|
| Choosing compatible versions | Starters and managed dependency versions | You declare what you need, and Boot supplies tested versions. |
| Repetitive bean setup | Auto-configuration | Beans are created when the classpath and settings indicate you need them. |
| Separate server install | Embedded web server | The server is a library inside your application. |
| Scattered settings | Externalized configuration | One property system, with overrides from files, environment variables, and command-line arguments. |
Boot calls itself opinionated. It makes default choices, such as Tomcat as the web server and Logback for logging, and expects you to override them only when you have a reason.
Starters: Curated Dependency Sets
A starter is a dependency whose job is to pull in other dependencies. You add one starter and receive a set of libraries that are known to work together.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>Neither dependency declares a <version>. The Boot parent POM (or Gradle plugin) manages versions for every library it knows about. Choosing the Boot version therefore chooses the whole library set.
spring-boot-starter-web brings in Spring MVC, JSON support through Jackson, and an embedded Tomcat. spring-boot-starter-test brings in JUnit, Mockito, AssertJ, and Spring's test support. A starter contains almost no code of its own. It is a dependency list.
A later chapter in this tutorial covers starters in detail. For now, remember that a starter changes the classpath, and the classpath drives the next mechanism.
Auto-Configuration: Beans Chosen by Conditions
Auto-configuration is the mechanism that creates beans for you. Boot ships many auto-configuration classes. Each one is a normal Spring @Configuration class guarded by conditions, such as:
@ConditionalOnClass: apply only if a given class is on the classpath.@ConditionalOnMissingBean: apply only if you have not defined a bean of that type.@ConditionalOnProperty: apply only if a property has a given value.
If spring-boot-starter-web puts Spring MVC on the classpath, the web auto-configuration registers a DispatcherServlet and message converters. If you remove that starter, those beans never appear.
Your Beans Win
@ConditionalOnMissingBean gives you a predictable override rule. If you define a bean of the same type, Boot's version backs off.
package com.example.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JsonConfig {
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper().findAndRegisterModules();
}
}Boot's own ObjectMapper is not created here, because yours exists.
Seeing What Boot Decided
Auto-configuration is not magic, and you can inspect it. Start the application with the debug flag and Boot prints a conditions report. The report lists which auto-configurations matched and which did not, with the reason.
java -jar target/demo-0.0.1-SNAPSHOT.jar --debugThe same switch works as a property:
debug=trueWhen a bean you expected is missing, or a bean you did not expect appears, this report is the first place to look. The "Negative matches" section tells you which condition failed.
The Embedded Web Server
With a traditional deployment, the server is the host and your application is the guest. Boot reverses that. Tomcat is a library on your classpath, and your main method starts it. The result is one process started with java -jar.
This has practical consequences:
- The same artifact runs on a laptop, in CI, and in a container. No server installation is required.
- The server version is part of your dependency set and changes when you upgrade Boot.
- You configure the server through Boot properties, for example
server.port, rather than through server-specific XML files. - You can swap the server. Excluding the Tomcat starter and adding the Jetty or Undertow starter changes it without touching controller code.
Boot can still produce a WAR for deployment into an external container, but that is the less common path and needs extra setup.
The Entry Point in One Look
All three mechanisms start from one class.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}SpringApplication.run creates the application context, applies auto-configuration, starts the embedded server if the application is a web application, and then keeps running. @SpringBootApplication turns on auto-configuration and component scanning. A later chapter explains the annotation in detail.
What Boot Does Not Do
Knowing the limits prevents wrong expectations.
- It does not hide Spring. When something fails, the stack trace and the concepts are Spring's. Understanding beans, scopes, and DI still matters.
- It does not generate your application logic. It removes setup code, not design decisions.
- It does not guarantee small or fast applications. Auto-configuration adds startup work, and each starter adds libraries. A large starter set means a larger JAR and a longer startup.
- It is not required for Spring. You can still use the Spring Framework alone, which gives full control at the cost of writing the configuration yourself.
Common Errors at This Stage
- Expecting a bean that never appears. The cause is usually a missing starter or library on the classpath, so the
@ConditionalOnClasscheck fails. Check the conditions report. - Two beans of one type after adding your own. Boot backs off only for the type its condition checks. If your bean has a different type or a different name than the one the condition inspects, both can exist.
- Mixing versions manually. Overriding a managed version works, but it takes the library out of the tested set. Do it only for a specific reason, such as a security fix.
- Putting the main class in the wrong package. Component scanning starts from the main class's package, so classes outside it are not found. Later chapters on project structure cover this.
Exercise
A teammate adds spring-boot-starter-web to a project and sees a DispatcherServlet bean without writing any configuration. Then they define their own ObjectMapper bean and notice that spring.jackson.* properties stop affecting JSON output. Explain both observations and name the tool for confirming the first one.
Show answer
The starter puts Spring MVC and Jackson on the classpath. Boot's web auto-configuration is guarded by @ConditionalOnClass, so it matches and registers the DispatcherServlet. Boot's Jackson auto-configuration creates its ObjectMapper only under @ConditionalOnMissingBean. Once the teammate defines an ObjectMapper, Boot's version backs off, and the spring.jackson.* properties, which configure Boot's version, no longer apply to the custom one. They can confirm which auto-configurations matched by starting the application with --debug (or debug=true) and reading the conditions report.
Exercise
Name two problems of pre-Boot Spring applications and the Boot mechanism that addresses each.
Show answer
Any two of these pairs are correct. Incompatible library versions are addressed by starters and managed dependency versions. Repetitive bean configuration is addressed by auto-configuration. Installing and keeping in sync a separate servlet container is addressed by the embedded web server.