Understanding Spring Boot’s Main Class and Key Annotations
This article explains how Spring Boot’s automatically generated Application class serves as the entry point, details the @SpringBootApplication annotation and its composed annotations, and shows how to customize component scanning and disable unwanted auto‑configurations using @ComponentScan and @EnableAutoConfiguration with practical code examples.
Spring Boot automatically generates a class named SpringbootRunApplication (or similar) that contains a main method; running this method starts the embedded server and the application.
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class SpringbootRunApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRunApplication.class, args);
}
}The single annotation present on this class, @SpringBootApplication, is a meta‑annotation that combines @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan. Its source defines four configurable attributes: exclude, excludeName, scanBasePackages, and scanBasePackageClasses. exclude: exclude specific auto‑configuration classes. excludeName: exclude by class name. scanBasePackages: specify packages to scan. scanBasePackageClasses: specify classes whose packages should be scanned.
Because @SpringBootApplication aggregates the three annotations, you can replace it with any combination of @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan when needed.
@ComponentScan
@ComponentScantells Spring where to look for components, configurations and services. If no attributes are supplied, it scans the package of the annotated class and its sub‑packages. The default resourcePattern is “**/*.class”.
@ComponentScan(basePackages = "com.secbro2.controller")Explicitly setting basePackages (or using @SpringBootApplication(scanBasePackages = "...")) is useful when the project structure does not follow the default convention and components are placed in unexpected locations.
@EnableAutoConfiguration
This annotation enables Spring Boot’s auto‑configuration mechanism, which configures beans based on the classes found on the classpath. It works best with “starter” dependencies that bundle required libraries and default settings.
When an auto‑configuration is not desired—for example, when a data‑source starter is present but no database is used—you can disable it with the exclude attribute of @SpringBootApplication:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)Why use Spring Boot?
Spring Boot replaces verbose XML configuration with Java annotations, bundles an embedded servlet container (Tomcat by default), and follows the “convention over configuration” principle through auto‑configuration. Adding a starter dependency automatically pulls in required libraries and sensible defaults, allowing developers to focus on business logic.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
