@SpringBootApplication Annotation
1. Definition
@SpringBootApplication is a meta-annotation in Spring Boot that marks the main class of a Spring Boot application.
It is a combination of three annotations:
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan
Spring Boot automatically:
1. Marks the class as a configuration class (@SpringBootConfiguration)
2. Enables Spring Boot's auto-configuration (@EnableAutoConfiguration)
3. Scans the package for components (@ComponentScan)
2. Breakdown of Each Part
1 2 3
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan
Specialized version of This is the heart of Spring Boot. Tells Spring where to look for
@Configuration. components (@Component,
It tells Spring Boot to automatically
Tells Spring that this class can be @Service, @Repository,
configure beans for you based on:
used as a source of bean @Controller, @RestController,
Classpath settings etc.).
definitions.
Available dependencies By default, it scans the package
Equivalent to writing a Java-
based Spring configuration class. Properties defined in of the main class and its sub-
[Link] / packages.
[Link]
< That's why the main class is
If spring-boot-starter-web is in your usually placed in the root package of
dependencies, Boot automatically the project structure.
configures:
DispatcherServlet
Tomcat server (by default)
Jackson for JSON processing
3. Flow of Execution
When you run:
[Link]([Link], args);
Here's what happens:
01 02
Creates ApplicationContext (Spring container) Scans packages (due to @ComponentScan)
03 04
Loads configurations (due to Applies auto-configurations (due to
@SpringBootConfiguration) @EnableAutoConfiguration)
05 06
Starts embedded server (if it's a web app, e.g., Application is ready
Tomcat/Jetty/Undertow)
4. Customizing Behavior & Visual Representation
Customizing Behavior 5. Visual Representation
You can disable parts of it:
@SpringBootApplication
|
@SpringBootApplication(exclude = +-- @SpringBootConfiguration --> Marks as configuration
{[Link]}) class
public class MyApplication
|
{ +-- @EnableAutoConfiguration --> Auto-configures
public static void main(String[] args)
Spring beans
{ |
[Link]([Link], args); +-- @ComponentScan --> Scans components in package
}
}
Summary: @SpringBootApplication = Configuration
Or restrict component scanning: + Auto-Configuration + Component Scan
It makes Spring Boot projects run with just one
@SpringBootApplication(scanBasePackages =
annotation instead of writing multiple annotations
{"[Link]", "[Link]"})
manually.
public class MyApplication {
...
}