Sub topic 9
The main class and @SpringBootApplication
This chapter assumes you can read basic Java, know what a Spring bean is, and have generated a project with Spring Initializr. It explains the one class every Spring Boot project starts from and the annotation on it.
The generated entry point
Initializr creates a class named after your project. It holds the main method and one annotation.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}main is a normal Java entry point. It calls SpringApplication.run, which creates the application context, runs auto-configuration, registers your beans, and starts the embedded server if the project has a web starter. The first argument tells Spring which class holds the configuration. The second passes command-line arguments, such as --server.port=9000, to Spring's configuration system.
What @SpringBootApplication combines
@SpringBootApplication is a shortcut for three annotations:
| Annotation | What it does |
|---|---|
| `@SpringBootConfiguration` | Marks the class as a configuration class, so it can declare `@Bean` methods. |
| `@EnableAutoConfiguration` | Turns on auto-configuration, which creates beans based on the libraries on your classpath. |
| `@ComponentScan` | Finds classes annotated with `@Component`, `@Service`, `@Repository`, `@RestController` and similar, and registers them as beans. |
You could write the three annotations yourself and get the same behavior. The single annotation is the convention.
Why the package location matters
@ComponentScan with no arguments scans the package of the annotated class and all its sub-packages. Here that means com.example.demo and everything below it. Auto-configuration also uses this package as its default base for things like entity scanning.
The diagram below shows which classes are found.
This leads to the most common beginner error. If you put a controller in com.other, Spring never registers it. The application starts without an error, but the endpoint returns 404.
Customizing the annotation
You can adjust the annotation when you need to. Two attributes come up most often.
@SpringBootApplication(
scanBasePackages = {"com.example.demo", "com.other"},
exclude = {org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class}
)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}scanBasePackagesreplaces the default scan location with the packages you list.excludestops a specific auto-configuration class from applying. This is useful when a dependency on the classpath triggers configuration you do not want.
Common errors
- Endpoint returns 404 although the controller exists. The controller is outside the main class's package tree. Move it, or set
scanBasePackages. - The app fails because a database URL is missing. A database starter is on the classpath, so auto-configuration tries to create a
DataSource. Add the connection properties, or excludeDataSourceAutoConfigurationif you do not need a database. - Two classes carry
@SpringBootApplication. Both scan overlapping packages and can register the same beans twice. Keep one per application.
Exercise
Your main class is in com.example.demo. You add a @RestController in com.example.api. The application starts, but GET requests to its endpoint return 404. Why, and what are two ways to fix it?
Show answer
@ComponentScan only covers com.example.demo and its sub-packages, and com.example.api is a sibling package, so the controller is never registered as a bean. Fix it either by moving the controller into com.example.demo or one of its sub-packages, or by setting scanBasePackages on @SpringBootApplication to include com.example.api.