Sub topic 2
What a Starter Is and What It Brings In
This chapter assumes you can create and run a basic Spring Boot application with Maven or Gradle. It also assumes you know what a bean and the application context are, and that you have read the previous chapter on the Boot parent and BOM.
What a starter is
A starter is a dependency descriptor. It is a published artifact that contains no application code, only a POM (or Gradle module metadata) listing other dependencies. When you declare one starter, your build tool resolves everything it lists, including transitive dependencies.
The starter does not configure anything. It only puts libraries on the classpath. Creating beans from those libraries is the job of auto-configuration, which later chapters cover. Keeping the two separate matters when you debug. A missing class is a starter or dependency problem. A missing bean is usually an auto-configuration or condition problem.
Declaring a starter
You declare a starter like any other dependency. No version is needed, because the Boot parent or BOM manages it.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>In Gradle, with the Spring Boot plugin applied, the equivalent is:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}What spring-boot-starter-web brings in
The web starter pulls in a tree of artifacts. At the top level it brings in these groups:
| Dependency | Purpose |
|---|---|
| `spring-boot-starter` | The core starter shared by almost all others |
| `spring-boot-starter-json` | Jackson for JSON conversion |
| `spring-boot-starter-tomcat` | The embedded Tomcat server |
| `spring-web`, `spring-webmvc` | The Spring MVC framework |
The core spring-boot-starter in turn brings in spring-boot, spring-boot-autoconfigure, spring-boot-starter-logging (Logback by default), and a YAML parser. Notice that spring-boot-autoconfigure arrives through almost every starter. That jar holds the auto-configuration classes that react to what the starters put on the classpath.
Inspecting the dependency tree
Do not rely on memory for what a starter contains, because the contents change between Boot versions. Ask your build tool:
mvn dependency:treeFor Gradle, run ./gradlew dependencies --configuration runtimeClasspath. The output shows each transitive artifact and the version the BOM selected.
Swapping a transitive dependency
Because starters are only dependency lists, you can change what they bring in. The usual example is replacing Tomcat with Jetty. Exclude the Tomcat starter from the web starter, then add the Jetty starter:
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>No Java code changes. Auto-configuration sees Jetty classes instead of Tomcat classes and builds a Jetty server.
Common errors
- Adding a version to a Boot-managed starter. This overrides the BOM and can produce mismatched Spring versions. Omit the version unless you have a reason.
- Adding a library without its starter. If you add only
spring-webmvc, you lose the logging, JSON, and server dependencies that the starter would have supplied. - Mixing two web server starters. With both Tomcat and Jetty on the classpath, Boot picks one by its own precedence rules, which may not be the one you intended. Exclude the one you do not want.
- Treating a starter as the source of beans. If a bean is missing even though the starter is present, the cause is a condition in auto-configuration. Check the conditions report instead of the dependencies.
Exercise
You add spring-boot-starter-web to a project and run mvn dependency:tree. Which artifact in the tree contains the auto-configuration classes, and why does it appear without you declaring it?
Show answer
spring-boot-autoconfigure contains them. It arrives transitively through spring-boot-starter, which spring-boot-starter-web depends on. The starter itself holds no code, only the dependency list that pulls this jar in.
Exercise
Your application should use Jetty instead of Tomcat. What two changes do you make to the build file, and do you change any Java code?
Show answer
Exclude spring-boot-starter-tomcat from spring-boot-starter-web, and add spring-boot-starter-jetty as a dependency. No Java code changes are needed, because auto-configuration reacts to the Jetty classes on the classpath.