首页 > 编程知识 正文

get和go的区别(getResource和getResources的区别)

时间:2023-05-06 02:14:44 阅读:124072 作者:1791

getResource和getResources都将“加载包含当前类加载器和父类加载器的路径的资源文件”/** * 1.getResource *加载包含当前类加载器和父类加载器的路径! *例如,当前项目的类路径包含conf/demo.properties文件,部署的第三方jar包也包含此文件*当前项目下的此资源文件* */URL URL demo.properties )返回/** * 2.getResources *加载当前类加载器和父类加载器路径的资源文件*将返回找到的所有资源文件。 *例如,当前工程类的路径包含conf/demo.properties文件,而部署的第三方jar包也包含此文件。 *将所有这些文件恢复为*/enumerationurlenumeration=loader.get resources (conf/demo )//打印所有同名的资源文件while (enumeration ) system.out.println (' file=' URL1. getfile ) ); }例如,创建一个新的spring boot项目,并在resources目录中创建META-INF/spring.factories

pom.xml中引入的软件包。 许多软件包都有META-INF/spring.factories目录文件,在Spring boot启动时将加载所有这些jar软件包的META-INF/spring.factories project xmlns=' http://maven.Apache.org/POM/4.0.0 ' xmlns : xsi=' http://www.w3.org/2001/XML方案ins ttins maven-4.0.0.xsd ' model版本4.0.0/modelversionparentgroupidorder groupidartifactidspring-boot-starter-parararer --- lookupparentfromrepository---- parentdependenciesdependencygroupidorg.spring framework.boot/groupidartifactididsppory dependencydependencygroupidorg.spring framework.boot/groupidartifactidspring-boot-starter-test/art ifier scopeexclusionsexclusiongroupidorg.JUnit.vintage/groupidartifactidjunit-vintage-engine/artifact id/exclusclustion

@ springbootapplicationpublicclassresourceapplicationimplementscommandlinerunner { publicstaticvoidmain { string [ ] args } } @ overridepublicvoidrun (string . args ) throws exception (class loader loader=class utils.getdefaultclassloader ) ); URL URL=loader.get resource (meta-INF/spring.factories ); system.out.println (' resource : ' URL.getpath ) ); system.out.println ((=======================) () ) //打印所有同名资源文件while (enumeration.hasmoreelements ) ) URL1=enumeration.nextelement ); system.out.println (' resources : ' URL1. getfile ) ); } }

从上面的输出中可以看到,使用getResource时,只读取当前模块resources下的MATE-INF/spring.factories文件。 getResources将读取所有依赖的软件包resources下的MATE-INF/spring.factories文件。

Spring Boot在启动时使用它,并加载ApplicationContextInitializer以实现类初始化。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。