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)
- 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):
<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>
The importance of initialization of Application Context
Application below
<code>Context Type: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext</code>
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: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