Home > Java > javaTutorial > spring-: how-spring-boot-determines-application-context

spring-: how-spring-boot-determines-application-context

Susan Sarandon
Release: 2025-01-29 16:08:09
Original
385 people have browsed it

spring-: how-spring-boot-determines-application-context

Spring Boot automatically determines the implementation of Application Context

When calling , Spring Boot will automatically determine the correct ApplicationContext implementation according to the following factors.

: SpringApplication.run(MySpringBootApp.class, args); Class path (dependencies in the project)

  1. running application type (web or non -web)
  2. OK process SpringApplication internally use the following logic to select the appropriate ApplicationContext:

If Spring MVC or Spring Webflux (Spring-Boot-Starter-Web or Spring-Boot-Starter-WEBFLUX):

    It will initialize a
  • Web -based application context

    : Based on the service (Spring-Boot-Starter-Web):

      (Spring MVC applications for embedded Tomcat, Jetty or Undertow).
    • Reacting (Spring-Boot-Starter-Webflux):
    • (for WebFlux applications).
    • AnnotationConfigServletWebServerApplicationContext
    • If there is neither Spring-Boot-Starter-Web nor Spring-Boot-Starter-weight:
    • AnnotationConfigReactiveWebServerApplicationContext It will initialize a
    • non -web application above and below
    : →
  • .
  • WEB application context as examples
    • If it contains Spring-Boot-Starter-Web, the output will be: AnnotationConfigApplicationContext
    • Non -web application context examples
  • If you remove the Spring-Boot-Starter-Web, the output will become:

<code class="language-java">@SpringBootApplication
public class WebApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(WebApplication.class, args);
        System.out.println("Context Type: " + context.getClass().getName());
    }
}</code>
Copy after login
The importance of initialization of Application Context

Application below
<code>Context Type: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext</code>
Copy after login
is

Core container

, it manages the life cycle and configuration of Bean in the Spring Boot application. Initialization is important for the following reasons:

1. Bean management

  • Application context Registration and management bean , allowing dependency injection (@Autowired).
  • Without application context, Spring will not know how to instance and inject dependencies.

2. Automatic configuration

  • The mechanism @EnableAutoConfiguration depends on the application context.
  • It scan the path and configure the
  • Spring component according to the dependencies.
  • 3. Life cycle and event management

Application context publishes the life cycle event (
    ,
  • ). ApplicationReadyEvent ApplicationStartedEvent It listens to the shutdown signal and manage the resource correctly.
  • 4. Embedded web server support

For web applications, the application context starts
    embedded server
  • (Tomcat, Jetty, Undertow). Without it, Spring Boot
  • Can't handle HTTP request
  • .
  • 5. Environment and attribute management

context from or
    load
  • configuration attribute application.properties. application.yml It manages configuration files () and settings specific to the environment.
  • @Profile
  • The actual impact of the correct Application Context

Choosing the correct application above and below will affect the behavior of

Application

, as follows:

1. Determine whether to start the embedded web server

  • If you choose the wrong context, your application may not be able to start as a web application.
  • Web application requires AnnotationConfigServletWebServerApplicationContext, it guides Tomcat/Jetty.

2. Control component scanning and dependence injection

  • The context is initialized and injected by the dependencies within its range.
  • For example, non -web context will not scan or initialize the controller (@RestController will not work).

3. Enable or disable automatic configuration

  • Spring Boot Based on the selected context Automatic application configuration .
  • For example: If the web context is selected, the Spring Boot will automatically configure the MVC component.

4. Affecting the management and loading method of bean

  • Web context pre -configured DispatcherServlet, it handles the HTTP request.
  • Non -web context does not have, which means that If there is no additional configuration, you will not be able to handle the web request .

Summary

---
方面 Application Context 的影响
Bean 管理 初始化和管理依赖项 (`@Autowired`)
Web 服务器 启动嵌入式 Tomcat/Jetty(如果为 Web 上下文)
自动配置 根据类路径应用自动配置
生命周期管理 处理启动/关机事件
配置文件和环境 加载属性,管理配置文件 (`@Profile`)
依赖注入范围 确定哪些 Bean 和控制器可用
The last idea

Spring Boot will automatically select the correct application above and below according to the class path

.
  • Web application Need to be based on the context of web ().
  • Non -web application The standard -based context using standard (). AnnotationConfigServletWebServerApplicationContext
  • The correct context of initialization
  • ensure the correct dependency injection, automatic configuration and life cycle management. ?

The above is the detailed content of spring-: how-spring-boot-determines-application-context. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template