公司动态
使用Spring的ClassPathResource加载资源文件
参考博客链接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来获取。