0% found this document useful (0 votes)
3 views4 pages

Understanding @SpringBootApplication Annotation

@SpringBootApplication is a meta-annotation in Spring Boot that combines @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan, marking the main class of a Spring Boot application. It enables automatic configuration and component scanning, simplifying the setup of Spring applications. The flow of execution involves creating an ApplicationContext, scanning packages, loading configurations, applying auto-configurations, and starting the embedded server.

Uploaded by

jagansenthil51
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Understanding @SpringBootApplication Annotation

@SpringBootApplication is a meta-annotation in Spring Boot that combines @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan, marking the main class of a Spring Boot application. It enables automatic configuration and component scanning, simplifying the setup of Spring applications. The flow of execution involves creating an ApplicationContext, scanning packages, loading configurations, applying auto-configurations, and starting the embedded server.

Uploaded by

jagansenthil51
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

@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 {
...
}

You might also like