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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

See all articles