Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一个小BUG #2

Open
banmao999 opened this issue Mar 16, 2023 · 2 comments
Open

一个小BUG #2

banmao999 opened this issue Mar 16, 2023 · 2 comments

Comments

@banmao999
Copy link

banmao999 commented Mar 16, 2023

感谢大佬的分享,源码看得头疼,这个简易的项目更方便分析流程

不过有个小问题,这个问题与 SpringBoot 3.0.3 版本中的BUG类似,都是路径中存在空格等字符,经过 classLoader.getResource 方法后变成了Unicode编码

SpringBoot仓库对应Issue:spring-projects/spring-boot#34379

在本项目中对应的位置:com.mafei.spring.MaFeiApplicationContext#scanBeanDefinition

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// 取得绝对路径: /Users/mafei007/AppProjects/IdeaProjects/spring_study/out/production/simple_impl/com/mafei/test
URL resource = classLoader.getResource(path);
File file = new File(resource.getFile());

// 此处进行解码即可
String absolutePath = file.getAbsolutePath();
file = new File(URLDecoder.decode(absolutePath, StandardCharsets.UTF_8));

// 遍历目录下的所有文件,都是 componentScan 需要扫描的,这里只遍历了一层目录
@banmao999
Copy link
Author

另外在 SpringBoot 3.0.4 中修复这个BUG的方式,虽然也是通过 URLDecoder.decode 进行解码,但是在解码之前他先将路径中的空格替换为 %20 字符,这个行为我不理解,为什么需要这一步,想请教下大佬有没有思路?

源码定位:org.springframework.core.io.support.PathMatchingResourcePatternResolver#convertClassLoaderURL

image

@mafei007even
Copy link
Owner

@banmao999 chatGPT的回答:

在 URI 中,空格是一个不允许出现的字符,它在 URI 解析时会被解释为 URI 的结束符或分隔符,这可能会导致 URI 解析错误。
为了避免这种错误,URI 规范定义了一些保留字符(如 "%20")作为代替空格的转义字符,它们表示特定的字符或符号,使得它们在 URI 中具有特殊的含义而不被解释为结束符或分隔符。
因此,将空格替换为 "%20" 是将字符串转换为有效的 URI 格式的必要步骤,以确保 URI 的正确解析和处理。如果不替换空格或使用其他合适的转义字符,则可能会导致 URI 不被正确解析和处理。

这个bug不修了,就这样吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants