Card 02/ 06
@SpringBootApplication: The Three Annotations Inside It
One annotation that switches on configuration, auto-configuration and component scanning together.
@SpringBootApplication is a single annotation that combines three others: @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan. Putting it on the main class is the same as putting all three there.
Reading the annotation's own definition by reflection (Java's way of inspecting classes while the program runs) lists the three:
SpringBootConfiguration
EnableAutoConfiguration
ComponentScan| Annotation | Job |
|---|---|
@SpringBootConfiguration | Marks the class as a source of bean definitions; a specialised @Configuration |
@EnableAutoConfiguration | Turns on the mechanism that creates beans from what is on the classpath |
@ComponentScan | Finds your own @Component-family classes, starting from this class's package |
Two of the three exist in plain Spring already: @Configuration, which @SpringBootConfiguration specialises, and @ComponentScan. @EnableAutoConfiguration is the one Boot adds, and it is the mechanism that creates beans for you.
@ComponentScan starts from the package of the class it is on, which is why the main class usually sits in the application's top package. A @Service in com.rides.pricing is found when the main class is in com.rides, and missed when the main class is in com.rides.app. @SpringBootConfiguration also has a second use: Boot's test support searches for it to find your application's configuration.
So the three are separate switches under one name. You can widen the scan, exclude one auto-configuration (@SpringBootApplication(exclude = ...)), or add configuration, each through its own part and without giving up the other two.
← → move through sections and cards