The main components of the SpringBoot container

The main components of the SpringBoot container

SpringBoot is a rapid development framework based on Spring Framework, which can help developers quickly build independent, production-level, deployable applications. SpringBoot provides an embedded Tomcat container as the default web container, and also supports other web containers and application servers, such as Jetty, Undertow, WebSphere, etc. In a SpringBoot application, the container is a very important component that is responsible for managing the life cycle of the application, processing requests and responses, managing the life cycle of objects, and more. This article will introduce the main components of the SpringBoot container and their functions.

insert image description here

The main components of the SpringBoot container

The main components of the SpringBoot container include:

  1. SpringApplication
  2. ApplicationContext
  3. DispatcherServlet
  4. WebMvcConfigurer
  5. Filter
  6. Servlet
  7. EmbeddedServletContainer

The functions of these components and how to use them are described below.

1. SpringApplication

SpringApplication is the core class of the SpringBoot application, which is responsible for starting the SpringBoot application, and is responsible for initializing and configuring the ApplicationContext container of the Spring Framework. SpringApplication provides multiple static methods to create and start SpringBoot applications according to different needs.

The following is a simple sample code that demonstrates how to use SpringApplication to start a SpringBoot application:

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

In the above sample code, we first added the @SpringBootApplication annotation, which is a combined annotation, including @Configuration, @EnableAutoConfiguration and @ComponentScan annotations. We then started the SpringBoot application using the SpringApplication.run() method, which accepts two parameters: the application's main class and command-line arguments.

2. ApplicationContext

ApplicationContext is the core container of Spring Framework, which is responsible for managing and organizing each Bean object in the application, and provides functions such as dependency injection, AOP, and event mechanism. In a SpringBoot application, the ApplicationContext is created and initialized by the SpringApplication class.

The following is a simple sample code that demonstrates how to obtain an ApplicationContext instance:

@SpringBootApplication
public class MyApp {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context = SpringApplication.run(MyApp.class, args);
        MyBean myBean = context.getBean(MyBean.class);
        myBean.doSomething();
    }
}

In the above sample code, we first use the SpringApplication.run() method to start the SpringBoot application and get the ApplicationContext instance. Then, we use the ApplicationContext.getBean() method to obtain a Bean instance named MyBean, and call its doSomething() method.

3. DispatcherServlet

DispatcherServlet is the core component of Spring Framework's Web MVC framework. It is responsible for processing HTTP requests and responses, distributing requests to corresponding Controllers, and returning Controller responses to clients. In a SpringBoot application, the DispatcherServlet is created and initialized by Spring MVC auto-configuration.

The following is a simple sample code that demonstrates how to use DispatcherServlet to handle HTTP requests and responses:

@Controller
public class MyController {
    
    
    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
    
    
        return "Hello World!";
    }
}

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

In the above sample code, we first defined a Controller called MyController, which handles GET requests for the path /hello and returns the string "Hello World!". Then, we started the SpringBoot application, which automatically created and initialized the DispatcherServlet, and registered MyController to the DispatcherServlet.

4. WebMvcConfigurer

WebMvcConfigurer is the configuration interface of Spring MVC, which provides multiple methods that can be used to configure various options of the Spring MVC framework. In the SpringBoot application, WebMvcConfigurer is created and initialized by Spring MVC automatic configuration, and the functions of the Spring MVC framework can be extended and customized by implementing this interface.

The following is a simple sample code that demonstrates how to use WebMvcConfigurer to configure options for the Spring MVC framework:

@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {
    
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
    
    
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
}

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

In the above sample code, we first define a configuration class named MyWebMvcConfigurer, which implements the WebMvcConfigurer interface and overrides the addCorsMappings() method for configuring cross-origin resource sharing (CORS) options. Then, we started the SpringBoot application, which automatically created and initialized the DispatcherServlet, and registered MyWebMvcConfigurer to the DispatcherServlet.

5. Filter

Filter is one of the core components of Java Servlet API. It is responsible for processing HTTP requests and responses, and can add various logic processing between requests and responses. In a SpringBoot application, Filter can be implemented by implementing the Filter interface or inheriting the OncePerRequestFilter class.

The following is a simple sample code that demonstrates how to use Filter to process HTTP requests and responses:

@Component
public class MyFilter implements Filter {
    
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
    
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        System.out.println("Request URL: " + httpRequest.getRequestURL());
        chain.doFilter(request, response);
    }
}

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

In the above sample code, we first define a Filter named MyFilter, which will print the requested URL when processing HTTP requests. Then, we register MyFilter in the container of the SpringBoot application. After the application starts, it will automatically create and initialize Filter, and register it in the embedded Tomcat container.

6. Servlet

Servlet is one of the core components of Java Servlet API, which is responsible for handling HTTP requests and responses. In a SpringBoot application, a Servlet can be implemented by implementing the Servlet interface or inheriting the HttpServlet class.

The following is a simple sample code that demonstrates how to use Servlets to handle HTTP requests and responses:

public class MyServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        resp.getWriter().write("Hello World!");
    }
}

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

In the above sample code, we first define a Servlet named MyServlet, which will return the string "Hello World!" when handling HTTP requests. Then, we register MyServlet in the container of the SpringBoot application. After the application starts, it will automatically create and initialize the Servlet, and register it in the embedded Tomcat container.

7. EmbeddedServletContainer

EmbeddedServletContainer is SpringBoot's embedded Web container, which is responsible for handling HTTP requests and responses, and managing the application life cycle. SpringBoot supports a variety of embedded web containers, such as Tomcat, Jetty, Undertow, etc.

The following is a simple sample code that demonstrates how to use EmbeddedServletContainer to start an embedded Tomcat container:

@SpringBootApplication
public class MyApp {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication app = new SpringApplication(MyApp.class);
        app.addListeners(new ApplicationPidFileWriter());
        ConfigurableApplicationContext context = app.run(args);
        EmbeddedServletContainer container = context.getBean(EmbeddedServletContainer.class);
        container.start();
    }
}

In the above sample code, we first created a SpringApplication instance, and added the ApplicationPidFileWriter monitor

Guess you like

Origin blog.csdn.net/JasonXu94/article/details/131303232