What a Starter Is
Before Spring Boot, adding a REST API to a project meant hunting down a dozen libraries: Spring MVC, a JSON library, a servlet container, a validation library, and a version of each that worked with all the others. One wrong version and the app failed at startup with an error nobody could explain. Starters replaced that hunt with a single line.
The rule
One starter, one feature: it brings the libraries, Boot brings the setup.
A starter is a dependency that contains almost no code of its own. Its job is to pull in a tested set of other libraries for one feature. For library-service's REST API:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>That one entry brings Spring MVC, Jackson for JSON, an embedded Tomcat server, and logging. You write no version number and no glue code.
Starters work hand in hand with auto-configuration (the next section). The starter puts libraries on the classpath; auto-configuration notices them and creates the beans they need. So adding spring-boot-starter-web is enough for the app to start a web server on port 8080 and serve your @RestControllers.
The name tells you who made it:
- Official starters are named
spring-boot-starter-<feature>, such asspring-boot-starter-web. - Third-party starters put their own name first,
<project>-spring-boot-starter, such asmybatis-spring-boot-starter. Thespring-bootprefix is reserved for the Spring team.
The same starters work with Gradle:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}The shortcut
When you need a feature, don't list libraries; name the feature and look for its starter. Then check that auto-configuration picked it up by starting the app and reading the log.
Name the feature
Reject a new
Bookwhose title is blank.
The trap is adding the underlying libraries by hand next to a starter, each with its own version. You end up with two versions of one library on the classpath, and the resulting errors are hard to trace. Let the starter decide.
Drills
Answer fast — trust the rule.
What does a Spring Boot starter mainly contain?
Which of these does spring-boot-starter-web bring?
A company writes its own starter for an internal audit library. What should it be called?
After adding a starter, what makes Spring create the beans the new libraries need?
Remember
- One starter, one feature: it brings the libraries, Boot brings the setup.
- Name the feature, find its starter, then confirm in the startup log.
- Trap: adding the starter's libraries again by hand with their own versions.
