Starters & Auto-Configuration: Smart Defaults

Starters & Auto-Configuration: Smart Defaults in Java: Before Spring Boot, starting a web project meant hand-picking a dozen compatible library versions and…

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

StarterGives you
spring-boot-starter-webREST controllers, JSON, embedded Tomcat
spring-boot-starter-data-jpaHibernate, repositories, transactions
spring-boot-starter-securityAuthentication and authorisation
spring-boot-starter-validation@Valid, @NotNull, and friends
spring-boot-starter-testJUnit 5, Mockito, AssertJ
spring-boot-starter-actuatorHealth 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 @RestController in 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

  1. 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.
  2. Pinning versions manually: Let the Boot parent POM manage versions. Overriding one library often breaks another.
  3. 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 --debug to print the auto-configuration report showing what was applied and what was skipped, and why.
  • Add Actuator early: A /actuator/health endpoint is what your deployment platform will probe.

Practice Exercises

  1. 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.
  2. The Report: Run your app with --debug and list five auto-configurations that were applied and five that were skipped.
  3. The Override: Change the server port to 9090 two different ways — via application.properties and via a bean — and confirm both work.