What is @SpringBootApplication annotation?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Spring Boot development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
@SpringBootApplication is a convenience annotation that combines three annotations: (1) @Configuration: marks the class as a source of bean definitions. Equivalent to a Spring XML configuration file — methods annotated with @Bean create Spring-managed beans; (2) @EnableAutoConfiguration: enables Spring Boot's auto-configuration mechanism. Tells Spring Boot to automatically configure the application based on classpath dependencies and existing beans; (3) @ComponentScan: enables component scanning — Spring searches the package (and sub-packages) of the annotated class for Spring components (@Component, @Service, @Repository, @Controller, @RestController). Usage: @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }. Customization: @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) — exclude specific auto-configurations. @SpringBootApplication(scanBasePackages = {"com.example.service", "com.example.repo"}) — customize scan packages. SpringApplication.run(): creates the application context, triggers auto-configuration, starts the embedded server, and deploys the application. Returns ConfigurableApplicationContext. Main class location: put in the root package (e.g., com.example) so @ComponentScan includes all sub-packages automatically.
Common Mistake
Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Spring Boot experience.