Skip to content

Commit dda90a0

Browse files
committed
spring context
1 parent 4c63d09 commit dda90a0

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
## Spring有哪些Context
2+
Spring 里带 “Context” 的类很多,但日常真正会手动 new 出来或配置在 web.xml 里的只有 **6 个**。把它们按“运行环境”和“配置方式”两条轴一摆,就知道该选谁。
3+
4+
---------------------------------
5+
1. 纯 Java SE / 单元测试
6+
---------------------------------
7+
| 实现类 | 特点 | 典型场景 |
8+
|---|---|---|
9+
| **AnnotationConfigApplicationContext** | 注解驱动,零 XML;自动扫描 `@Component``@Configuration` | 单元测试、Spring Boot 早期启动器、微服务 jar |
10+
| **GenericApplicationContext** | 完全空壳,需手动 `registerBeanDefinition``registerBean` | 需要动态编程注册 Bean 的框架二次开发、测试 |
11+
| **GenericXmlApplicationContext** | 只读 XML,不刷新多次 | 快速起一个 XML 配置的原型,极少用 |
12+
13+
---------------------------------
14+
2. Web 环境(Servlet)
15+
---------------------------------
16+
| 实现类 | 特点 | 典型场景 |
17+
|---|---|---|
18+
| **XmlWebApplicationContext** | 传统 web.xml 指定 `contextConfigLocation` 的 XML | 老项目、遗留系统 |
19+
| **AnnotationConfigWebApplicationContext** | 注解驱动,web.xml 里写 `<context-param>` 指向 `@Configuration`| Spring MVC 无 XML 项目、Spring Boot 内嵌容器 |
20+
| **GroovyWebApplicationContext** | 支持 Groovy DSL 脚本 | 极小众,内部脚手架/脚本化部署 |
21+
22+
---------------------------------
23+
3. 响应式 Web(Spring WebFlux)
24+
---------------------------------
25+
| 实现类 | 特点 | 典型场景 |
26+
|---|---|---|
27+
| **ReactiveWebApplicationContext**(接口) | 由 Spring Boot 自动选择 `AnnotationConfigReactiveWebServerApplicationContext` | 响应式 Netty/Undertow 应用 |
28+
29+
---------------------------------
30+
4. 一张决策表(怎么选?)
31+
32+
| 场景 | 推荐 Context |
33+
|---|---|
34+
| 写单元测试 | `AnnotationConfigApplicationContext` |
35+
| 写框架/工具,需要动态注册 Bean | `GenericApplicationContext` |
36+
| 老项目 web.xml 还在用 XML | `XmlWebApplicationContext` |
37+
| Spring MVC 新项目无 XML | `AnnotationConfigWebApplicationContext` |
38+
| Spring Boot 默认 |`SpringApplication.run()` 自动选择:<br>• servlet → `AnnotationConfigServletWebServerApplicationContext`<br>• reactive → `AnnotationConfigReactiveWebServerApplicationContext` |
39+
40+
---------------------------------
41+
一句话记忆
42+
- **注解驱动**`AnnotationConfig*ApplicationContext`
43+
- **XML 驱动**`Xml*ApplicationContext`
44+
- **动态注册**`GenericApplicationContext`
45+
- **WebFlux**`Reactive*ApplicationContext`
46+
47+
48+
## SpringContext中有哪些东西
49+
50+
把 Spring 所有 **ApplicationContext**(无论哪种实现)抽象出来,它们都继承同一棵接口树,因此拥有 **一组通用能力****一批核心托管对象**。背住下面这张“九宫格”,就掌握了 Context 的全部底牌。
51+
52+
──────────────────
53+
一、通用功能(对外 API)
54+
──────────────────
55+
1. **IoC 容器**
56+
`getBean()` / `getBeansOfType()` / `containsBean()` … 直接面向开发者。
57+
58+
2. **生命周期管理**
59+
`start()` / `stop()` / `refresh()` / `close()`,支持 SmartLifecycle 回调。
60+
61+
3. **事件广播**
62+
`publishEvent()` + `@EventListener`(或 `ApplicationListener`),内部使用 `ApplicationEventMulticaster`
63+
64+
4. **国际化(i18n)**
65+
`getMessage(code, args, locale)`,底层持有一个 `MessageSource`
66+
67+
5. **资源加载**
68+
`getResource("classpath:...")``getResource("file:...")`,统一协议,底层是 `ResourceLoader`
69+
70+
6. **Environment 抽象**
71+
`getEnvironment().getProperty(...)`,整合 JVM 参数、系统变量、YAML/Properties、Profile。
72+
73+
──────────────────
74+
二、核心托管对象(内部持有的“大管家”)
75+
──────────────────
76+
| 成员对象 | 接口/类 | 主要能力 |
77+
|---|---|---|
78+
| **ConfigurableEnvironment** | Environment | 统一读取配置、激活 Profile |
79+
| **BeanFactory** | ConfigurableListableBeanFactory | 真正实例化、注入、缓存 Bean |
80+
| **BeanDefinitionRegistry** | 同上 | 注册/删除 BeanDefinition(配方) |
81+
| **ApplicationEventMulticaster** | ApplicationEventMulticaster | 事件广播(同步/异步) |
82+
| **ResourceLoader** | DefaultResourceLoader | 按协议加载文件、URL、类路径资源 |
83+
| **MessageSource** | DelegatingMessageSource | 支持 i18n 文本解析 |
84+
| **ConversionService** | ConversionService | 类型转换(String → Date 等) |
85+
| **PropertyResolver** | PropertySourcesPropertyResolver | 占位符 `${}` 解析 |
86+
| **TypeConverter** | SimpleTypeConverter | 字段/参数级类型转换 |
87+
88+
──────────────────
89+
三、关系图(一句话)
90+
```
91+
ApplicationContext 门面
92+
│ 对外暴露“九宫格”功能
93+
94+
├─组合→ Environment
95+
├─组合→ BeanFactory(Registry)
96+
├─组合→ ApplicationEventMulticaster
97+
├─组合→ ResourceLoader
98+
└─组合→ MessageSource
99+
```
100+
101+
因此,无论你在用
102+
`AnnotationConfigApplicationContext``GenericApplicationContext` 还是
103+
`AnnotationConfigServletWebServerApplicationContext`,它们都 **统一拥有** 上述 6 大通用功能和 9 个核心托管对象,差别只在“**配置来源**”(注解、XML、Groovy、编程式)和“**运行环境**”(独立 JVM、Servlet、Reactive)。

0 commit comments

Comments
 (0)