springboot类加载关系以及资源加载bug分析

加载器关系

当使用 tomcat 容器时,运行时上下文类加载器是 TomcatEmbeddedWebappClassLoader,其父类加载器是 LaunchedURLClassLoader。在IDEA中运行时,上下文类加载器是 AppClassLoader,即应用程序类加载器。

TomcatServletWebServerFactory设置TomcatEmbeddedWebappClassLoader类加载的时候,会设置parentclassloader即LaunchedURLClassLoader,因为项目是通过LaunchedURLClassLoader加载器加载的,如下代码可测

@SpringBootApplication
public class SpringbootApplication {
    /**
     * java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005
     *
     * @param args
     */
    public static void main(String[] args) {
        ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
        System.out.println("SpringbootApplication类装载器:" + currentClassLoader);
        ClassLoader oneParentClassLoader = currentClassLoader.getParent();
        System.out.println("SpringbootApplication的父类加载器——扩展类加载器:" + oneParentClassLoader);
        ClassLoader twoClassLoader = oneParentClassLoader.getParent();
        System.out.println("SpringbootApplication的父类的父类加载器——引导类加载器:" + twoClassLoader);
        SpringApplication.run(SpringbootApplication.class, args);
//        SpringbootApplication类装载器:org.springframework.boot.loader.LaunchedURLClassLoader@238e0d81
//        SpringbootApplication的父类加载器——扩展类加载器:sun.misc.Launcher$AppClassLoader@55f96302
//        SpringbootApplication的父类的父类加载器——引导类加载器:sun.misc.Launcher$ExtClassLoader@3d646c37
    }
}

问题注意点

jar启动时候:读取resources下的xlsx文件时候,fis.available()=0,txt文本可以正常读取

idea启动时候:任何文件都可以

结论:jar启动读取xlsx时候使用LaunchedURLClassLoader的RandomAccessDataFile$DataInputStream进行流包装,没有重写available方法,即available=0,这里是JarFileEntries方法,普通文件使用ZipInflaterInputStream进行包装,像xlsx就不会

getMethod说明:所述getMethod()函数是java.util.zip包的一部分。该函数返回作为参数传递的特定ZipEntry的压缩方法;如果未指定,则返回-1。这里我是真没明白,到底啥意思
参考:https://www.geeksforgeeks.org/java-zipentry-getmethod-function-with-examples/

InputStream getInputStream(FileHeader entry) throws IOException {
    if (entry == null) {
        return null;
    } else {
        InputStream inputStream = this.getEntryData(entry).getInputStream();
        if (entry.getMethod() == 8) {
            inputStream = new ZipInflaterInputStream((InputStream)inputStream, (int)entry.getSize());
        }

        return (InputStream)inputStream;
    }
}

栗子

Thread thread = Thread.currentThread();
ClassLoader contextClassLoader = thread.getContextClassLoader();
if (contextClassLoader != null) {
    fis = contextClassLoader.getResourceAsStream(filePath);
}
// int length = fis.available();// 这个判断不准,直接用下边read即可
//            while (length > 0) {
//                int hasRead = fis.read(bytes, 0, Math.min(length, BUFFER_LENGTH));
//                response.getOutputStream().write(bytes, 0, hasRead);
//                length -= hasRead;
//            }
int bytesRead;
while ((bytesRead = fis.read(bytes)) != -1) {
  response.getOutputStream().write(bytes, 0, bytesRead);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值