Table of Contents
If Spring MVC or Spring Webflux (Spring-Boot-Starter-Web or Spring-Boot-Starter-WEBFLUX):
Core container
1. Bean management
2. Automatic configuration
Application
1. Determine whether to start the embedded web server
2. Control component scanning and dependence injection
3. Enable or disable automatic configuration
4. Affecting the management and loading method of bean
Spring Boot will automatically select the correct application above and below according to the class path
Home Java javaTutorial spring-: how-spring-boot-determines-application-context

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

Jan 29, 2025 pm 04:08 PM

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:

@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());
    }
}
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1254
29
C# Tutorial
1228
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

See all articles