Sub topic 2
What Spring Boot Is and the Problems It Solves
This chapter assumes you know basic Java, have used Maven or Gradle as a user, understand HTTP at a basic level, and have a JDK and a terminal available. It explains what Spring Boot is and which setup problems it removes. Later chapters cover project generation and running the app.
Background: Spring Framework and dependency injection
The Spring Framework is a Java library for building applications out of objects called beans. A bean is an object whose creation and wiring Spring manages. Dependency injection (DI) is the technique Spring uses: instead of a class creating its collaborators with new, Spring passes them in, usually through the constructor.
public class OrderService {
private final PaymentClient paymentClient;
public OrderService(PaymentClient paymentClient) {
this.paymentClient = paymentClient;
}
}The container that creates and wires beans is the application context. Plain Spring is powerful, but you must tell the context which beans exist, which versions of libraries work together, and how to start a web server. That setup is where Spring Boot helps.
What Spring Boot is
Spring Boot is an opinionated layer on top of the Spring Framework. "Opinionated" means it picks sensible defaults for you, and you override them only when you need to. It does not replace Spring. Your code still uses the same annotations, beans, and DI.
Before Boot, a typical web project needed these steps:
- Choose compatible versions of many libraries by hand.
- Write XML or Java configuration for the web layer, data sources, and more.
- Build a WAR file and deploy it to an external server such as Tomcat.
Boot reduces each step with three main features.
| Problem | Boot's answer |
|---|---|
| Finding compatible library versions | Starters and managed dependencies |
| Writing repetitive configuration | Auto-configuration |
| Installing and configuring a server | Embedded web server |
Starters: curated dependency sets
A starter is a single dependency that pulls in a tested group of libraries for one purpose. The Spring Boot parent in your build file manages the versions, so you normally leave out the version number.
<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>Here spring-boot-starter-web brings in Spring MVC, JSON support, and an embedded server. spring-boot-starter-test brings in the testing libraries, and <scope>test</scope> keeps them out of the production package.
Auto-configuration
Auto-configuration is Boot's mechanism for creating beans based on what it finds on the classpath. If the web starter is present, Boot configures Spring MVC. If a database driver is present, Boot can configure a data source once you supply connection properties.
Two rules explain most of its behavior:
- Auto-configuration is conditional. A configuration applies only when its conditions hold, such as a class being present.
- Your own beans take priority. If you define a bean of the same kind, Boot backs off and uses yours.
The @SpringBootApplication annotation on your main class switches this on. A later chapter covers it in detail.
The embedded web server
With the web starter, Boot includes Tomcat as a library inside your application. The application starts the server itself, so you do not install or configure one separately. The result is a self-contained program you can start with java -jar.
The diagram below shows how the pieces relate.
Common mistakes
- Thinking Boot is a different framework. It is Spring with defaults, so Spring documentation still applies.
- Adding a version to Boot-managed dependencies. This can cause conflicts with the versions Boot has tested.
- Assuming auto-configuration is magic. It is ordinary conditional configuration, and you can inspect and override it.
Exercise
A project adds spring-boot-starter-web and no other web-related dependency, yet it can serve HTTP requests without Tomcat being installed. Which two Boot features make this possible?
Show answer
The starter puts Spring MVC and an embedded Tomcat on the classpath, and auto-configuration sees them and creates the beans needed to run the web layer. The embedded server runs inside the application, so no external installation is needed.
Exercise
You define your own bean of a type that Boot would normally auto-configure. What does Boot do?
Show answer
Boot backs off and uses your bean, because auto-configuration is conditional and user-defined beans take priority.