@@ -191,7 +191,7 @@ public String[] selectImports(AnnotationMetadata annotationMetadata) {
191
191
192
192
该方法调用链如下:
193
193
194
- ![ ] ( https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 3c1200712655443ca4b38500d615bb70~tplv-k3u1fbpfcp-watermark.image )
194
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 3c1200712655443ca4b38500d615bb70~tplv-k3u1fbpfcp-watermark.png )
195
195
196
196
现在我们结合` getAutoConfigurationEntry() ` 的源码来详细分析一下:
197
197
@@ -239,27 +239,27 @@ AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoC
239
239
spring-boot/spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories
240
240
```
241
241
242
- ![ ] ( https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 58c51920efea4757aa1ec29c6d5f9e36~tplv-k3u1fbpfcp-watermark.image )
242
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 58c51920efea4757aa1ec29c6d5f9e36~tplv-k3u1fbpfcp-watermark.png )
243
243
244
244
从下图可以看到这个文件的配置内容都被我们读取到了。` XXXAutoConfiguration ` 的作用就是按需加载组件。
245
245
246
- ![ ] ( https://oss.javaguide.cn/p3-juejin /94d6e1a060ac41db97043e1758789026~tplv-k3u1fbpfcp-watermark.png )
246
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring /94d6e1a060ac41db97043e1758789026~tplv-k3u1fbpfcp-watermark.png )
247
247
248
248
不光是这个依赖下的` META-INF/spring.factories ` 被读取到,所有 Spring Boot Starter 下的` META-INF/spring.factories ` 都会被读取到。
249
249
250
250
所以,你可以清楚滴看到, druid 数据库连接池的 Spring Boot Starter 就创建了` META-INF/spring.factories ` 文件。
251
251
252
252
如果,我们自己要创建一个 Spring Boot Starter,这一步是必不可少的。
253
253
254
- ![ ] ( https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 68fa66aeee474b0385f94d23bcfe1745~tplv-k3u1fbpfcp-watermark.image )
254
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 68fa66aeee474b0385f94d23bcfe1745~tplv-k3u1fbpfcp-watermark.png )
255
255
256
256
** 第 4 步** :
257
257
258
258
到这里可能面试官会问你:“` spring.factories ` 中这么多配置,每次启动都要全部加载么?”。
259
259
260
260
很明显,这是不现实的。我们 debug 到后面你会发现,` configurations ` 的值变小了。
261
261
262
- ![ ] ( https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 267f8231ae2e48d982154140af6437b0~tplv-k3u1fbpfcp-watermark.image )
262
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 267f8231ae2e48d982154140af6437b0~tplv-k3u1fbpfcp-watermark.png )
263
263
264
264
因为,这一步有经历了一遍筛选,` @ConditionalOnXXX ` 中的所有条件都满足,该类才会生效。
265
265
@@ -295,27 +295,27 @@ public class RabbitAutoConfiguration {
295
295
296
296
第一步,创建` threadpool-spring-boot-starter ` 工程
297
297
298
- ![ ] ( https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 1ff0ebe7844f40289eb60213af72c5a6~tplv-k3u1fbpfcp-watermark.image )
298
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 1ff0ebe7844f40289eb60213af72c5a6~tplv-k3u1fbpfcp-watermark.png )
299
299
300
300
第二步,引入 Spring Boot 相关依赖
301
301
302
- ![ ] ( https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 5e14254276604f87b261e5a80a354cc0~tplv-k3u1fbpfcp-watermark.image )
302
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 5e14254276604f87b261e5a80a354cc0~tplv-k3u1fbpfcp-watermark.png )
303
303
304
304
第三步,创建` ThreadPoolAutoConfiguration `
305
305
306
- ![ ] ( https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 1843f1d12c5649fba85fd7b4e4a59e39~tplv-k3u1fbpfcp-watermark.image )
306
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 1843f1d12c5649fba85fd7b4e4a59e39~tplv-k3u1fbpfcp-watermark.png )
307
307
308
308
第四步,在` threadpool-spring-boot-starter ` 工程的 resources 包下创建` META-INF/spring.factories ` 文件
309
309
310
- ![ ] ( https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 97b738321f1542ea8140484d6aaf0728~tplv-k3u1fbpfcp-watermark.image )
310
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 97b738321f1542ea8140484d6aaf0728~tplv-k3u1fbpfcp-watermark.png )
311
311
312
312
最后新建工程引入` threadpool-spring-boot-starter `
313
313
314
- ![ ] ( https://oss.javaguide.cn/p3-juejin /edcdd8595a024aba85b6bb20d0e3fed4~tplv-k3u1fbpfcp-watermark.png )
314
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring /edcdd8595a024aba85b6bb20d0e3fed4~tplv-k3u1fbpfcp-watermark.png )
315
315
316
316
测试通过!!!
317
317
318
- ![ ] ( https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ 9a265eea4de742a6bbdbbaa75f437307~tplv-k3u1fbpfcp-watermark.image )
318
+ ![ ] ( https://oss.javaguide.cn/github/javaguide/system-design/framework/spring/ 9a265eea4de742a6bbdbbaa75f437307~tplv-k3u1fbpfcp-watermark.png )
319
319
320
320
## 总结
321
321
0 commit comments