The life cycle of the Spring Boot container

The life cycle of the Spring Boot container

When developing with Spring Boot, we often need to understand and master the life cycle of the Spring container. This article will introduce the life cycle of the Spring Boot container, including the process of container creation, initialization, destruction, etc., and provide corresponding code examples.

insert image description here

The life cycle of the Spring Boot container

The life cycle of a Spring Boot container includes the following main stages:

  1. Container creation: When the application starts, Spring Boot will create a Spring container, which is responsible for managing all components in the application (including beans, configuration files, etc.).

  2. Container initialization: After the container is created, Spring Boot will initialize the components in the container, including reading configuration files, parsing annotations, and instantiating beans.

  3. Running of the application: After the container is initialized, the application starts running, processing user requests, executing business logic, and so on.

  4. Destruction of the container: When the application is closed, Spring Boot will destroy the components in the container, release resources, etc.

Below we will introduce the details of each of these stages and provide corresponding code examples.

container creation

When developing with Spring Boot, we usually define the main method in the Application class, and start the application through the SpringApplication.run() method in this method. During this process, Spring Boot will automatically create a Spring container and load all configuration files and bean definitions. Here is an example:

@SpringBootApplication
public class DemoApplication {
    
    

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

}

In this example, we marked the Application class with @SpringBootApplicationannotations , and called SpringApplication.run()the method in the main method to start the application. This method creates a Spring container and automatically loads all configuration files and bean definitions.

Container initialization

After the container is created, Spring Boot initializes the components in the container. This process includes loading configuration files, parsing annotations, instantiating beans, etc. Here is an example:

@Configuration
public class AppConfig {
    
    

    @Bean
    public DataSource dataSource() {
    
    
        // 创建数据源
        return new DataSource();
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
    
    
        // 创建 JdbcTemplate
        return new JdbcTemplate(dataSource);
    }

}

In this example, we annotated the AppConfig class with @Configurationannotations and defined two beans in the class. The dataSource() method returns a data source, the jdbcTemplate() method returns a JdbcTemplate object, and depends on the data source returned by the dataSource() method. During container initialization, Spring Boot will automatically call these methods to instantiate the corresponding beans.

application running

After the container initialization is complete, the application starts running, processing user requests, executing business logic, and so on. Here is an example:

@RestController
public class HelloController {
    
    

    @GetMapping("/hello")
    public String hello() {
    
    
        return "Hello, world!";
    }

}

In this example, we use @RestControllerannotations to annotate the HelloController class, and define a hello() method in the class to handle /hello requests. When the application is running, when the user sends a /hello request, Spring Boot will automatically call this method and return the "Hello, world!" string.

container destruction

When the application is closed, Spring Boot will destroy the components in the container, release resources, etc. Here is an example:

@Configuration
public class ShutdownConfig {
    
    

    @Bean
    public ApplicationRunner applicationRunner(ConfigurableApplicationContext context) {
    
    
        return args -> {
    
    
            // 添加关闭钩子
            context.addApplicationListener(new ApplicationListener<ContextClosedEvent>() {
    
    
                @Override
                public void onApplicationEvent(ContextClosedEvent event) {
    
    
                    // 在容器关闭前执行必要的清理工作
                    System.out.println("应用程序即将关闭...");
                }
            });
        };
    }

}

In this example, we use @Configurationannotations to annotate the ShutdownConfig class, and define an applicationRunner() method in the class, which returns an ApplicationRunner object. In this method, we add a shutdown hook to perform necessary cleanup before the container is shut down. When the application shuts down, Spring Boot will automatically call this hook and perform the corresponding cleanup.

Summarize

This article introduces the life cycle of the Spring Boot container, including the stages of container creation, initialization, operation, and destruction. At the same time, we also provide corresponding code examples to help readers better understand and master the life cycle of the Spring Boot container.

In actual development, it is very important to understand and master the life cycle of the Spring Boot container, which can help developers better manage and optimize applications. Therefore, we recommend that developers carefully study and master the knowledge about the life cycle of the Spring Boot container when using Spring Boot for development.

Guess you like

Origin blog.csdn.net/2302_77835532/article/details/131303109