Spring Boot - entry class and @SpringBootApplication annotation

Entry class and @SpringBootApplication annotation

In a Spring Boot application, there are two important elements: the entry class and the @SpringBootApplication annotation.

Entry class (Main Class):

The entry class is the startup class of the Spring Boot application, which contains the main method as the entry point of the application.
In the entry class, you can start the Spring Boot application by calling the static run method of the SpringApplication class. This class is usually located
under the main package (base package) and is annotated with @SpringBootApplication.

Example:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootStudy2023Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootStudy2023Application.class, args);
    }

}

In the above example, SpringBootDemoApplication is the entry class, marked with @SpringBootApplication annotation.
The main method calls the run method of SpringApplication to start the application.

@SpringBootApplication annotation:

@SpringBootApplication is a composite annotation that integrates multiple annotations to simplify the configuration of Spring Boot applications. It contains the functions of the following three annotations:

@Configuration:表示该类是一个配置类,定义了Spring Bean的配置。
@EnableAutoConfiguration:启用Spring Boot的自动配置机制,根据项目的依赖自动配置Spring Bean。
@ComponentScan:自动扫描并加载被@Component及其派生注解修饰的类,使其成为Spring容器的Bean。

Using the @SpringBootApplication annotation can reduce a lot of configuration code and simplify the startup process of Spring Boot applications.

The entry class in the example uses the @SpringBootApplication annotation, indicating that it is the entry class of a Spring Boot application, and enables automatic configuration and component scanning.

Guess you like

Origin blog.csdn.net/qq_43116031/article/details/131029564