hugo/content/posts/1.Learning/A3.Reflections/Java/springboot-problem.md
2026-03-18 22:36:05 +08:00

43 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Springboot问题汇总"
date: 2023-10-14T21:43:48+08:00
tags: ["java"]
categories: ["Learning", "Reflections"]
---
### 1. 编写starter时引用后无法获取到bean
> 环境使用版本Springboot3.1.4版本
问题之前写starter的时候一直是按照网上的教程大部分是Springboot2.x版本但自己使用了最新版本Springboot3.1.4版本,所以一直不成功。
深入代码查看后,发现以下跟老版本不一致。
新版本:
```
SpringBootApplication --> EnableAutoConfiguration --> AutoConfigurationImportSelector.getCandidateConfigurations --> ImportCandidates.load --> LOCATION --> "META-INF/spring/%s.imports"
```
老版本:
```
SpringBootApplication --> EnableAutoConfiguration --> AutoConfigurationImportSelector.getCandidateConfigurations --> SpringFactoriesLoader.loadFactoryNames --> Factories_RESOURCE_LOCATION --> "META-INF/spring.factories"
```
所以按照新版 ` META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports` 进行编写的,可以正常识别。
注意:
这里的内容也不一样了,之前是需要写入一行,新版本支持多行格式
```java
Enumeration<URL> urls = findUrlsInClasspath(classLoaderToUse, location);
List<String> importCandidates = new ArrayList<>();
while (urls.hasMoreElements()) { // 如果有多行直接支持了把所有的都加入到importCandidates中去了
URL url = urls.nextElement();
importCandidates.addAll(readCandidateConfigurations(url));
}
```