Concept Explanation
Before Spring Boot, starting a web project meant hand-picking a dozen compatible library versions and writing hundreds of lines of XML. Boot replaced that with two ideas.
Starters are curated dependency bundles. One line pulls in everything you need at versions guaranteed to work together:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
That single entry brings Spring MVC, Jackson for JSON, validation, and an embedded Tomcat server.
Auto-configuration then inspects your classpath and configures sensible defaults. Tomcat on the classpath means Boot starts a web server on port 8080 — you wrote no configuration at all.
Starters Worth Knowing
| Starter | Gives you |
|---|---|
spring-boot-starter-web | REST controllers, JSON, embedded Tomcat |
spring-boot-starter-data-jpa | Hibernate, repositories, transactions |
spring-boot-starter-security | Authentication and authorisation |
spring-boot-starter-validation | @Valid, @NotNull, and friends |
spring-boot-starter-test | JUnit 5, Mockito, AssertJ |
spring-boot-starter-actuator | Health checks and metrics |
The Entry Point
@SpringBootApplication
public class ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}
@SpringBootApplication is three annotations in one:
@Configuration— this class may declare beans.@EnableAutoConfiguration— apply the classpath-based defaults.@ComponentScan— discover@Component,@Service,@Repository, and@RestControllerin this package and below.
Overriding a Default
Auto-configuration always yields to your own definition. Declare a bean of the same type and Boot backs off:
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper().findAndRegisterModules();
}
Most auto-configuration is written with @ConditionalOnMissingBean, which is exactly what makes this work.
Common Mistakes
- Main class in the wrong package: Component scanning starts at the main class's package. Put it at the root, or components in sibling packages are invisible.
- Pinning versions manually: Let the Boot parent POM manage versions. Overriding one library often breaks another.
- Fighting auto-configuration with XML: If a default is wrong, define a bean or set a property — do not reintroduce XML config.
Best Practices
- Keep the main class at the package root so scanning reaches everything.
- Use
--debugto print the auto-configuration report showing what was applied and what was skipped, and why. - Add Actuator early: A
/actuator/healthendpoint is what your deployment platform will probe.
Practice Exercises
- The Minimal API: Generate a project at start.spring.io with only the Web starter and get it responding on port 8080 without writing any configuration.
- The Report: Run your app with
--debugand list five auto-configurations that were applied and five that were skipped. - The Override: Change the server port to 9090 two different ways — via
application.propertiesand via a bean — and confirm both work.