zoukankan      html  css  js  c++  java
  • Class.getResource和ClassLoader.getResource的区别

    项目结构
    ┌─src
    │ └─main
    │ └─java
    │ └─com.guitu18.blog
    │ ├─doudou
    │ │ └─GetResourceTest.java
    │ └─SpringbootApplication.java
    └─resource
    ├─mapper
    │ └─BlogDao.xml
    └─confog.properties


    1、Class.getResource()可以从当前 Class 所在包的路径开始匹配获取资源,也可以从 classpath 根路径开始匹配获取资源


    1.1、Class.getResource()从当前包所在路径获取资源时不能以"/"开头// file:/E:/repo/guitu-blog/target/test-classes/com/guitu18/blog/doudou/

    System.out.println(this.getClass().getResource(""));


    1.2、而从 classpath 根路径获取资源时必须以"/"开头;
    // file:/E:/repo/guitu-blog/target/test-classes/
    System.out.println(this.getClass().getResource("/"));


    2、ClassLoader().getResource()只能从 classpath 根路径开始匹配获取资源

      2.1、ClassLoader().getResource()不能以"/"开头,且路径总是从 classpath 根路径开始;
         // file:/E:/repo/guitu-blog/target/test-classes/
       System.out.println(this.getClass().getClassLoader().getResource(""));
       // null
       System.out.println(this.getClass().getClassLoader().getResource("/"));


  • 相关阅读:
    逐步实现python版wc命令
    Linux中短横线(-)小记
    memcached启动脚本(class练习)
    nginx启动脚本(class练习)
    Python-类的方法
    re模块
    shutil模块
    时间模块(time/date)
    迭代器
    生成器
  • 原文地址:https://www.cnblogs.com/cndou/p/14918958.html
Copyright © 2011-2022 走看看