# Auto-Configuration and the Main Class — Spring Boot

Source: https://www.skillbyai.com/en/spring-boot/i-autoconfig

> Beans created for you, conditionally.

## @SpringBootApplication

`@SpringBootApplication` combines configuration, **component scanning** of its package and below, and **auto-configuration**. Auto-configuration classes create beans conditionally: with the JPA starter and H2 on the classpath, Boot configures a DataSource, an EntityManagerFactory and transactions; with webmvc it starts Tomcat and configures JSON with Jackson. Your own beans take precedence. Run with `--debug` (or the conditions report) to see which auto-configurations applied and why.

## The application class

This file is from a demo "shop" service generated by start.spring.io for Spring Boot 4.1.1 and Java 21 (Temurin 21.0.12), built with Maven 3.9.16; the project compiled and its tests passed. Typed properties are enabled here; seeding lives in a separate component (see the testing section for why).

```java
package com.example.shop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(ShopProperties.class)
public class ShopApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShopApplication.class, args);
    }
}
```

## Startup log lines, run

I packaged the demo with mvn package, started it with java -jar on port 8085 and called it with curl; the output is copied from that run. The embedded Tomcat started on the chosen port and the context was ready in about 5 seconds.

```bash
java -jar target/shop-0.0.1-SNAPSHOT.jar --server.port=8085
```

Output:

```
Tomcat started on port 8085 (http) with context path '/'
Started ShopApplication in 5.003 seconds (process running for 5.452)
```

**Quiz:** What decides which auto-configurations apply?

- [ ] Only the main method arguments
- [ ] Alphabetical order
- [ ] A random choice
- [x] Conditions such as classes on the classpath, existing beans and properties

*Answer:* Conditions such as classes on the classpath, existing beans and properties. Conditional configuration.
