资讯动态

使用Spring的ClassPathResource加载资源文件

发布时间:2026/10/11 17:32:13 来源:尧图企业网站定制
参考博客链接https://www.cnblogs.com/ruiati/p/6225093.htmlpublicclassTestClassPathResource{TestpublicvoidtestResouce(){ClassLoader loaderThread.currentThread().getContextClassLoader();System.out.println(loader.getResource().getPath());System.out.println(this.getClass().getResource().getPath());System.out.println(this.getClass().getResource(/).getPath());System.out.println(System.getProperty(user.dir));try{String filePathnewClassPathResource(../resources/application.yml).getFile().getAbsolutePath();System.out.println(filePath);}catch(IOExceptione){e.printStackTrace();}}}Class.getResource()获取的是相对于当前类的相对路径Class.getResource()获取的是classpath的根路径ClassLoader.getResource()获取的是classpath的根路径在创建ClassPathResource对象时我们可以指定是按Class的相对路径获取文件还是按ClassLoader来获取。

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑