spring_reference/X. Appendices/D.2. Spring Boot’s “JarFile...

18 lines
1015 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 附录D.2. Spring Boot的"JarFile"类
Spring Boot用于支持加载内嵌jars的核心类是`org.springframework.boot.loader.jar.JarFile`。它允许你从一个标准的jar文件或内嵌的子jar数据中加载jar内容。当首次加载的时候每个JarEntry的位置被映射到一个偏移于外部jar的物理文件
```java
myapp.jar
+---------+---------------------+
| | /lib/mylib.jar |
| A.class |+---------+---------+|
| || B.class | B.class ||
| |+---------+---------+|
+---------+---------------------+
^ ^ ^
0063 3452 3980
```
上面的示例展示了如何在myapp.jar的0063处找到A.class。来自于内嵌jar的B.class实际可以在myapp.jar的3452处找到B.class可以在3980处找到图有问题
有了这些信息我们就可以通过简单的寻找外部jar的合适部分来加载指定的内嵌实体。我们不需要解压存档也不需要将所有实体读取到内存中。