📘 Premium Read: Access my best content on
Medium member-only articles
— deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed.
Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount):
Explore My Udemy Courses
— Learn through real-time, project-based development.
In this article, we will discuss one very important Spring Boot annotation that is @SpringBootApplication with an example.
YouTube Video
@SpringBootApplication Annotation Overview
@SpringBootApplication annotation indicates a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.
The below diagram shows an internal implementation of @SpringBootApplication Annotation:
Note that the above @SpringBootApplication annotation code internally uses @Configuration, @EnableAutoConfiguration, and @ComponentScan annotations with their default attributes.
@SpringBootApplication Annotation Example
We use this annotation to mark the main class of a Spring Boot application:
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication// same as @Configuration @EnableAutoConfiguration @ComponentScanpublicclassApplication {
publicstaticvoidmain(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.
The @SpringBootApplication annotation is a combination of the following three Spring annotations:
@Configuration
This annotation indicates that a configuration class declares one or more @Bean methods. These classes are processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
@ComponentScan
This annotation is used to specify the base packages to scan for spring beans/components.
@EnableAutoConfiguration
This annotation enables the magical auto-configuration feature of Spring Boot, which can automatically configure a lot of stuff for you.
SpringBootApplication Annotation Optional Elements
The following are the parameters accepted in the @SpringBootApplication annotation:
Class<?>[] exclude - Exclude specific auto-configuration classes such that they will never be applied.
String[] excludeName - Exclude specific auto-configuration class names such that they will never be applied.
Class<?>[] scanBasePackageClass - A type-safe alternative to scanBasePackages() for specifying the packages to scan for annotated components.
String[] scanBasePackages - Base packages to scan for annotated components.
@SpringBootApplication Annotation Features not Mandatory
So far we have seen that @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan but none of these features are mandatory and you may choose to replace this single annotation with any of the features that it enables. For instance, you may not want to use a component scan in your application:
In this example, the Application is just like any other Spring Boot application except that @Component annotated classes are not detected automatically and the user-defined beans are imported explicitly (see @Import).
Comments
Post a Comment
Leave Comment