Sub topic 1
Spring Boot Starters and Auto-Configuration: How Boot Wires Your Application
Prerequisites
Assume you know Spring beans, dependency injection, application contexts, Java configuration, and basic Maven dependency management.
Dependency management: the Boot parent and BOM
Spring Boot manages compatible library versions so you usually do not specify a version for each dependency. With Maven, the Boot parent provides dependency management, plugin defaults, and build settings:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
</parent>A project that cannot use the parent can import the Spring Boot BOM, or Bill of Materials. A BOM is a coordinated list of dependency versions:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>What a starter is and what it brings in
A starter is a dependency descriptor that groups libraries for a common task. spring-boot-starter-web, for example, brings Spring MVC, an embedded server, JSON support, and related dependencies. It does not itself create beans or configure the application. Auto-configuration performs that work.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>What `@SpringBootApplication` really enables
@SpringBootApplication combines three annotations:
@SpringBootConfiguration, which identifies the main configuration class.@EnableAutoConfiguration, which asks Boot to apply eligible auto-configuration.@ComponentScan, which finds components in the application package and its subpackages.
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}How auto-configuration creates beans from the classpath
Auto-configuration is conditional configuration. Boot checks available classes, properties, and existing beans. If the web classes are present, it can configure MVC infrastructure and an embedded server. If you define an equivalent bean, many configurations back off so your application choice wins.
Exercise
What is the difference between spring-boot-starter-web and @EnableAutoConfiguration?
Show answer
The starter adds related libraries; @EnableAutoConfiguration enables Boot's conditional configuration for libraries available on the classpath.