spring-: how-spring-boot-determines-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)
- running application type (web or non -web)
- 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
. -
-
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: -
If it contains Spring-Boot-Starter-Web, the output will be:
@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()); } }
<程> Application below
<code>Context Type: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext</code>
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 和控制器可用 |
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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]

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

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