What is @SpringBootApplication and what annotations does it combine?
Learn what @SpringBootApplication does, the three annotations it combines, how auto-configuration and component scanning work, with examples and interview tips.
Expected Interview Answer
@SpringBootApplication is a convenience meta-annotation placed on the main class that bootstraps a Spring Boot application by combining three annotations: @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan.
@SpringBootConfiguration marks the class as a source of bean definitions (a specialized @Configuration). @EnableAutoConfiguration tells Boot to auto-configure beans based on the classpath and property settings. @ComponentScan scans the package of the annotated class and its sub-packages for @Component, @Service, @Repository, and @Controller beans. Because scanning starts from the annotated class's package, the main class is conventionally placed at the root of the package hierarchy.
- Removes boilerplate by replacing three annotations with one
- Enables classpath-driven auto-configuration
- Automatically discovers your components via package scanning
- Encourages a sensible root-package project layout
- Attributes can be tuned (exclude, scanBasePackages) when defaults do not fit
AI Mentor Explanation
Think of @SpringBootApplication as a team captain who single-handedly handles three pre-match duties at once: declaring the playing eleven, setting the field automatically for each bowler, and scouting every player in the squad's dressing room so nobody capable is left out of selection.
Step-by-Step Explanation
Step 1
Annotate the main class
Place @SpringBootApplication on the class containing the public static void main method.
Step 2
Bootstrap the context
Call SpringApplication.run(App.class, args) to create and refresh the ApplicationContext.
Step 3
Auto-configuration kicks in
@EnableAutoConfiguration inspects the classpath and properties to register sensible default beans.
Step 4
Component scanning runs
@ComponentScan discovers stereotyped beans in the main class's package and below.
Step 5
Refine when needed
Use exclude, excludeName, or scanBasePackages attributes to override defaults for non-standard layouts.
What Interviewer Expects
- Naming all three combined annotations correctly
- Explaining what auto-configuration actually does
- Understanding why the main class sits in the root package
- Knowing it is a meta-annotation, not magic
- Awareness of how to exclude specific auto-configurations
Common Mistakes
- Listing only @Configuration and @ComponentScan and forgetting @EnableAutoConfiguration
- Confusing @SpringBootApplication with @SpringBootConfiguration
- Placing the main class in a deep sub-package so scanning misses components
- Believing it disables all manual configuration
- Thinking auto-configuration ignores your property settings
Best Answer (HR Friendly)
“@SpringBootApplication is a single shortcut annotation you put on the starting class of a Spring Boot app. It bundles three jobs — marking configuration, turning on automatic setup based on your libraries, and finding your app's components — so you write far less boilerplate.”
Code Example
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}Follow-up Questions
- What does @EnableAutoConfiguration do under the hood?
- How do you exclude a specific auto-configuration class?
- Why should the main class be in the root package?
- What is the difference between @Configuration and @SpringBootConfiguration?
- How can you customize the base packages that get scanned?
MCQ Practice
1. Which three annotations does @SpringBootApplication combine?
@SpringBootApplication is a meta-annotation combining @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan.
2. From which package does @ComponentScan start scanning by default?
@ComponentScan scans the package containing the annotated class and all sub-packages, which is why the main class sits at the root.
3. How do you prevent a specific auto-configuration from being applied?
The exclude (or excludeName) attribute lets you remove specific auto-configuration classes.
Flash Cards
What three annotations does @SpringBootApplication combine? — @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan.
What does @EnableAutoConfiguration do? — Registers default beans automatically based on the classpath and property settings.
Why is the main class in the root package? — @ComponentScan scans from that package downward, so a root location covers all sub-packages.
How do you exclude an auto-configuration? — Use the exclude or excludeName attribute of @SpringBootApplication.
Continue Learning
Related Interview Questions
What is the difference between @Component, @Service, @Repository, and @Controller in Spring?
easy
What are Spring bean scopes and how do singleton and prototype differ?
medium
What is the Spring bean lifecycle and what callbacks does it provide?
medium
What is the difference between @Autowired field, constructor, and setter injection?
medium