Sub topic 3
What @SpringBootApplication really enables
This chapter assumes you can create and run a Spring Boot application. It also assumes you know what beans, the application context, @Configuration and @Bean are. The goal is to see what the one annotation on your main class switches on, and which mistakes come from misunderstanding it.
One annotation, three meta-annotations
Every Boot application starts with a class like this:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}@SpringBootApplication is a convenience annotation. It is equivalent to placing three annotations on the class:
| Annotation | What it does |
|---|---|
| `@SpringBootConfiguration` | Marks the class as a configuration class. It is a specialization of `@Configuration`, so you can declare `@Bean` methods on it. |
| `@ComponentScan` | Scans the class's package and all sub-packages for `@Component`, `@Service`, `@Repository`, `@Controller` and `@Configuration` classes. |
| `@EnableAutoConfiguration` | Turns on auto-configuration, which adds beans based on the classpath and properties. |
You can replace @SpringBootApplication with these three and get the same behavior. You rarely should, but knowing the split helps when something goes wrong.
flowchart TDWhat @EnableAutoConfiguration does
@EnableAutoConfiguration is itself built from two parts:
@AutoConfigurationPackagerecords the main class's package. Libraries such as Spring Data use it to know where to look for your entities and repositories.@Import(AutoConfigurationImportSelector.class)runs a selector during context startup. The selector loads the list of auto-configuration candidates from the classpath, removes any you excluded, and imports the rest as configuration classes.
Each candidate then decides for itself, through conditions, whether to apply. The selector does not create beans. It only hands over candidates. How candidates are listed and how conditions work are covered in later chapters.
Why the main class's location matters
@ComponentScan starts at the package of the class that carries the annotation. Given this layout:
com.example.app.DemoApplication
com.example.app.web.HelloController
com.example.other.AuditServiceHelloController is found, but AuditService is not, because com.example.other is outside com.example.app. The usual symptom is a NoSuchBeanDefinitionException or a controller that returns 404. The fix is to move the main class to a common parent package. You can also set scanBasePackages on the annotation.
Do not put the main class in the default (unnamed) package. Scanning would then cover every class on the classpath, and Boot logs a warning about it.
Excluding, and what the scan skips
The annotation accepts attributes that forward to its meta-annotations:
@SpringBootApplication(
exclude = DataSourceAutoConfiguration.class,
scanBasePackages = "com.example")
public class DemoApplication { }exclude and excludeName go to @EnableAutoConfiguration. scanBasePackages goes to @ComponentScan.
The scan is configured with filters that skip auto-configuration classes. If component scanning picked them up, they would be processed as ordinary configuration, and their ordering and back-off logic would break. Keep your own configuration classes out of the auto-configuration list and let the scan find them.
Common errors
- Main class too deep. Beans in sibling packages are silently missed.
- Putting
@EnableAutoConfigurationon several classes. This is redundant and can confuse later exclusions. Use it once. - Treating
excludeas a switch for starters. Excluding an auto-configuration removes only its beans. The starter's dependencies stay on the classpath.
Exercise
Your main class is com.example.app.DemoApplication, and a @Service class lives in com.example.services. The service is never created. Name two ways to fix it.
Show answer
Move the main class to the common parent package com.example. Or keep it where it is and set scanBasePackages = {"com.example.app", "com.example.services"} on @SpringBootApplication.