Sub topic 3
Starters: curated dependency sets
This chapter assumes you know basic Java, can read a Maven pom.xml, and understand that a web application answers HTTP requests. It also assumes you know what dependency injection is from the previous background chapter.
What a starter is
A starter is a dependency that contains no code of its own. Its job is to pull in a group of related libraries that work well together. You add one starter to your build file, and Maven or Gradle resolves the rest through transitive dependencies.
Without starters, a web application needs you to pick a Spring MVC version, a JSON library, a validation library, and a servlet container, then check that all their versions are compatible. A starter replaces that list with one line.
Starters follow a naming convention. Official ones are named spring-boot-starter-*, where the last part says what the application does. For example, spring-boot-starter-web is for web applications and spring-boot-starter-test is for testing.
A dependencies block
<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 has a <version> element. The Spring Boot parent POM, which your project inherits from, manages the versions. That is why the file stays short and why upgrading Spring Boot upgrades every managed library at once.
What these two starters bring in
| Starter | Main contents | Scope |
|---|---|---|
| `spring-boot-starter-web` | Spring MVC, JSON support through Jackson, an embedded Tomcat server, logging | compile (default) |
| `spring-boot-starter-test` | JUnit, Spring Test, AssertJ, Mockito, JSONassert | test |
The test scope matters. Test libraries are available when you compile and run tests, but they are left out of the packaged application.
How starters fit with the rest of Boot
Starters and auto-configuration work as a pair. The starter decides which libraries are present. Auto-configuration, covered in the next chapter, inspects the classpath and sets up beans for whatever it finds. Adding spring-boot-starter-web is what leads Boot to start an embedded web server later on.
Inspecting what you actually get
You can see the full dependency tree to check what a starter added:
./mvnw dependency:treeThe output lists each starter and the libraries below it. This is the first thing to check when you see an unexpected class or a version conflict.
Common errors
- Adding a version to a managed dependency. Setting
<version>on a starter overrides the parent's choice and can cause mismatches with the other Spring libraries. Leave it out unless you have a specific reason. - Mixing starters that conflict. For example,
spring-boot-starter-webbrings Tomcat. Adding another server starter without excluding Tomcat leaves two servers on the classpath. - Forgetting the test scope. Without
<scope>test</scope>, test libraries end up in your production artifact. - Adding a starter "just in case". Each starter adds libraries and can trigger auto-configuration you did not plan for. For instance, adding a database starter without a database setting can stop the application from starting.
- Missing the parent. The versionless style only works when the project inherits from
spring-boot-starter-parentor imports the Spring Boot dependency management. Otherwise Maven reports a missing version.
Exercise
A teammate adds spring-boot-starter-web with <version>2.0.0</version> to a project that uses the Spring Boot parent. Why is this risky, and what should they do instead?
Show answer
The explicit version overrides the version the parent manages, so the starter may no longer match the other Spring libraries in the project, which can cause runtime errors. They should remove the <version> element and let the parent supply it. To change versions, they should upgrade the Spring Boot parent version.
Exercise
Which command shows every library that spring-boot-starter-web pulled into a Maven project?
Show answer
./mvnw dependency:tree