首页 > 代码库 > java Resource

java Resource

ClassPathResource:

  String resource = ""; //相对路径

  Resource resource = new ClassPathResource(resource);

  //获取当前类下,同一个包中的资源

  Resource resource = new ClassPathResource("文件名称",this.getClass());

FileSystemResource:

  String resource = "";

  //文件系统的真实路径

   Resource resource = new FileSystemResource(resource);

UrlResource:

    String resource = "httP://...";

      resource = "file://c:/applic...";

     Resource resource = new UrlResource("file://c:/applicationContext.xml");

ServletContextResource:

  需要在web环境中才可以

   Resource resource = new ServletContextResource(servletContext,"/WEB-INF/classes/applicationContext.xml");

 

我们在使用spring容器的时候,由于applicationContext是beanFactory子类,封装了更多方法功能供我们调用,所以在使用applicationContext时,会使用特定格式的字符串表示各种类型

classPath:loaded from the classpath

file:loaded as a URL,from the fileSystem

http:  loaded as a URL

none   /data/config.xml    Depends on the underlying ApplicationContext