Lesson 2 of 12 · 9 min · Spring core
Spring Boot starters and auto-configuration
What you will learn
- What Spring Boot adds on top of Spring
- What a starter is and the common ones, including the Boot 4 names
- How auto-configuration works and how it backs off
- Override or exclude auto-configuration, and see the conditions report
- A standard project layout and the Maven build
In simple words
Plain Spring is powerful but needs a lot of setup: choose a web server, add libraries, configure each one. Spring Boot is a ready-made starter kit. You say "I want a web application with a database" by adding two dependencies, and Boot picks sensible settings and wires everything up for you. If you want something different, you provide your own, and Boot steps aside.
Words to know
- Starter: a single dependency that pulls in a set of libraries for one purpose.
- Auto-configuration: Boot creating beans automatically based on what is on the classpath.
- Opinionated defaults: sensible choices Boot makes so you do not have to.
- Classpath: the set of libraries available to your running application.
Starters
A starter is a dependency descriptor that brings in everything needed for a feature, with compatible versions managed by Spring Boot's parent (spring-boot-starter-parent) or BOM, so you rarely write a version number.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.1.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
Common starters:
| Starter | Gives you |
|---|---|
spring-boot-starter-webmvc |
Spring MVC, embedded Tomcat, JSON (Jackson). In Boot 3 and earlier it was called spring-boot-starter-web |
spring-boot-starter-data-jpa |
Spring Data JPA with Hibernate and a connection pool |
spring-boot-starter-validation |
Bean Validation (Hibernate Validator) |
spring-boot-starter-security |
Spring Security |
spring-boot-starter-actuator |
health, metrics and other operational endpoints |
spring-boot-starter-test |
JUnit, AssertJ, Mockito and Spring test support |
spring-boot-starter-webmvc-test and other -test starters |
test support for a specific technology (Boot 4) |
spring-boot-starter-flyway / -liquibase |
database migrations (these now need their own starter in Boot 4) |
Boot 4 changes to know (from the official migration guide): the web starter was renamed to spring-boot-starter-webmvc (the old name is deprecated), Boot is now split into smaller modules (spring-boot-<technology>), each technology has its own test starter, and Jackson 3 (packages under tools.jackson) replaced Jackson 2. Older tutorials and code samples using spring-boot-starter-web or @MockBean need these updates. Boot 4 requires Java 17 or newer (21 or 25 recommended) and Spring Framework 7.
Create a project at start.spring.io (Spring Initializr): choose Maven or Gradle, Java version, dependencies; download and open in your IDE.
The entry point
@SpringBootApplication
public class ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}
@SpringBootApplication = @SpringBootConfiguration (a @Configuration) + @EnableAutoConfiguration + @ComponentScan. With spring-boot-starter-webmvc on the classpath, run starts an embedded Tomcat on port 8080 and your application is a runnable JAR: java -jar app.jar. No separate server installation is needed.
How auto-configuration works
Spring Boot ships many auto-configuration classes. Each is guarded by conditions that check the environment:
@ConditionalOnClass(DataSource.class): only if a class is on the classpath.@ConditionalOnMissingBean: only if you have not defined your own bean of that type.@ConditionalOnProperty: only if a property has a given value.@ConditionalOnWebApplication,@ConditionalOnBean, and others.
So with spring-boot-starter-data-jpa and a database driver (PostgreSQL) on the classpath, plus spring.datasource.url set, Boot creates a DataSource (HikariCP pool), an EntityManagerFactory, a transaction manager and Spring Data repositories, without you writing any of it. Add Jackson and Boot configures an JsonMapper for HTTP message conversion. Add Actuator and the health endpoint appears.
The key principle is that auto-configuration backs off. If you declare your own DataSource bean, Boot's @ConditionalOnMissingBean condition fails and yours is used instead:
@Bean
public DataSource dataSource() { // replaces the auto-configured one
// custom setup
}
Auto-configuration classes are registered in a file inside each module (META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports); your own libraries can provide them the same way, which is how third-party starters work.
Seeing what happened
- Run with
--debug(ordebug=trueinapplication.properties) to print the conditions evaluation report: which auto-configurations matched and why, and which did not and why. - The Actuator endpoint
/actuator/conditionsand/actuator/beansshow the same at run time (secure them). - IDEs show configuration properties with autocompletion (add
spring-boot-configuration-processorfor your own properties).
Excluding auto-configuration
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
or spring.autoconfigure.exclude=... in properties. Do this when a starter pulls in something you do not want (for example Spring Security's default login on a project that does not use it yet). Do not exclude by habit; prefer configuring through properties.
Project layout and the build
A common layout:
shop/
├── pom.xml
└── src
├── main
│ ├── java/com/shop/ShopApplication.java (main class in the root package)
│ ├── java/com/shop/orders/... (feature packages)
│ └── resources
│ ├── application.properties (or application.yml)
│ └── db/migration/ (Flyway SQL files)
└── test/java/com/shop/...
Keep the main class in the root package so component scanning reaches everything below it. Common commands (Maven):
./mvnw spring-boot:run # run in development
./mvnw test # run tests
./mvnw package # build target/shop-0.0.1-SNAPSHOT.jar
java -jar target/shop-0.0.1-SNAPSHOT.jar
Gradle: ./gradlew bootRun, ./gradlew build. Spring Boot DevTools adds automatic restart on code changes in development. For containers, Boot can build an OCI image with ./mvnw spring-boot:build-image, without a Dockerfile.
Actuator in one paragraph
Add spring-boot-starter-actuator for operational endpoints under /actuator. /actuator/health is used by load balancers and Kubernetes probes (liveness and readiness probes are on by default in Boot 4). Expose only what you need (management.endpoints.web.exposure.include=health,info) and secure the rest, because endpoints like env and heapdump can reveal secrets.
Common mistakes
- Using old starter names or
@MockBeanfrom Boot 3 tutorials with Boot 4. - Putting the main class in a sub-package, so scanning misses other packages.
- Fighting auto-configuration instead of overriding it with your own bean or property.
- Leaving unneeded starters that add default behaviour (a default security login).
- Exposing all Actuator endpoints publicly.
- Specifying versions that clash with Boot's managed versions.
Try it yourself
- Generate a project with
webmvc,data-jpa,validation,actuatorand PostgreSQL driver on start.spring.io. - Run with
--debugand find one positive match and one negative match in the conditions report. - Define your own
ClockorDataSourcebean and show that Boot backs off. - Build the JAR and run it with
java -jar; open/actuator/health.
Check yourself
What is a Spring Boot starter?
A single dependency that brings in a curated, version-compatible set of libraries for a purpose, such as web or JPA.
How does auto-configuration decide what to create?
Each auto-configuration class has conditions on the classpath, existing beans and properties; it applies only when they match, and backs off if you define your own bean.
How can you see why an auto-configuration was or was not applied?
Run with --debug to print the conditions evaluation report, or use the Actuator conditions endpoint.
What changed with the web starter in Spring Boot 4?
spring-boot-starter-web became spring-boot-starter-webmvc, and Boot split into smaller modules with per-technology test starters.
Interview questions
What is the use of @SpringBootApplication?
It combines @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan: it marks a configuration class, turns on auto-configuration and scans the package of the class and below.
How would you disable an auto-configuration?
Use exclude on @SpringBootApplication or @EnableAutoConfiguration, or set spring.autoconfigure.exclude.
What is the difference between Spring and Spring Boot?
Spring is the framework (container, MVC, data, security). Spring Boot adds starters, auto-configuration, embedded servers and production features so applications run with minimal configuration.
Summary
- Starters bundle dependencies; Boot manages compatible versions; Boot 4 renamed the web starter to
webmvcand added test starters. - Auto-configuration creates beans from the classpath and properties, and backs off when you define your own.
- Debug with
--debug; exclude only when needed; keep the main class in the root package. - Run the executable JAR with embedded Tomcat; secure Actuator endpoints.
Going deeper
- Spring Boot: Auto-configuration: how it works and how to replace it.
- Spring Boot: Build systems and starters: dependency management and the starter list.
- Spring Boot 4.0 Migration Guide: starter renames and removals.
- Spring Boot Actuator: operational endpoints.
See how much of this you can explain
A free mock interview asks you new questions on Spring Boot, REST and JPA for Java Full Stack Developer and scores every answer, with what to fix.