`

spring 资源访问 Resource

阅读更多

  在jdk的api中,资源访问基本上都是通过URL类和IO来完成的,首先我们来介绍一下jdk中的资源访问

  在j2se中,我们一般通过ClassLoader的getResource()方法来定位查找资源:

 

	public static void main(String[] args) throws IOException, URISyntaxException {
		URL url = Thread.currentThread().getContextClassLoader().getResource("format.properties");
		Properties p = new Properties();
		p.load(url.openStream());
		System.out.println(p.get("chengan"));
		System.out.println(url.getProtocol());
	}

   ClassLoader的getResource()方法访问资源有一个限制,资源的位置只能在classpath路径下或者classpath的子路径下,如果format.properties文件的路径不在classpath路径下,getResource()方法是找不到文件的,比如我们再开发web项目的时候,jdbc.properties一般都会放到WEB-INF下新建一个文件保存,此时在使用ClassLoader获取,就获取不到,下面我们就需要使用servletContext提供的方法,getResource()方法参数可以使用"/"作为路径,比如format.properties位于classpath路径下的test包下(getResource()方法参数的起始路径为classpath):

URL url = Thread.currentThread().getContextClassLoader().getResource("test/format.properties");

  

  在j2ee中,可以使用ServletContext提供的方法getResource()来访问资源:

		response.setContentType("text/html");
		URL url = request.getSession().getServletContext().getResource("/WEB-INF/config/jdbc.properties");
		response.getWriter().print(url);

  jdbc.properties文件放在WEB-INF的config文件下,servletContext的getResource方法以WebRoot为起始路径,上面代码运行的结果为:

  jndi:/localhost/SpringDemo/WEB-INF/config/jdbc.properties

 

spring的资源访问提供了统一的接口:Resource

spring在设计上使用了策略模式,针对不同的资源访问,提供了不同的实现类,常用的实现类有:

UrlResource:对java.net.URL访问的封装

ServletContextResource:对servletContext访问资源的封装

FileSystemResource:访问文件系统资源的封装

InputStreamResource:流操作资源访问

ContextResource:该接口是对访问容器资源的接口,提供一系列实现该接口的类

 

在spring中,访问资源的入口在applicationContext中,applicationContext继承了ResourcePatternResolver接口

public interface ResourcePatternResolver extends ResourceLoader {

	String CLASSPATH_ALL_URL_PREFIX = "classpath*:";

	Resource[] getResources(String locationPattern) throws IOException;

}

 ResourcePatternResolver又继承了ResourceLoader接口:

public interface ResourceLoader {

	/** Pseudo URL prefix for loading from the class path: "classpath:" */
	String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;


	Resource getResource(String location);

	ClassLoader getClassLoader();

}

 

在访问资源时,可以指定访问资源的方式:

	public Resource getResource(String location) {
		Assert.notNull(location, "Location must not be null");
                //常量CLASSPATH_URL_PREFIX值为:classpath:
		if (location.startsWith(CLASSPATH_URL_PREFIX)) {
			return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
		}
		else {
			try {
				// Try to parse the location as a URL...
				URL url = new URL(location);
				return new UrlResource(url);
			}
			catch (MalformedURLException ex) {
				// No URL -> resolve as resource path.
				return getResourceByPath(location);
			}
		}
	}

 上面是ResourceLoader的方法getResource()的默认实现,由上述代码可知,在指定资源的位置时,如果前缀指定为“classpath:",那么会使用ClassPathResource,否则默认使用UrlResource,如果出错会使用ClassPathResource

 

 

 

分享到:
评论

相关推荐

    spring中的资源访问宝典

    详细介绍了spring里各种对资源访问的支持。

    Spring实战之ServletContextResource访问资源文件示例

    主要介绍了Spring实战之ServletContextResource访问资源文件,结合实例形式分析了spring使用ServletContextResource读取与遍历资源文件相关操作技巧,需要的朋友可以参考下

    跟我学spring3(1-7)

    【第四章】 资源 之 4.3 访问Resource ——跟我学spring3 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3 【第五章】Spring...

    Spring Security OAuth2的resource_id配置和验证.docx

    Spring Security OAuth2 架构上分为Authorization Server认证服务器和Resource Server资源服务器。我们可以为每一个Resource Server(一个微服务实例)设置一个resourceid。Authorization Server给client第三方...

    Spring 2.0 开发参考手册

    17. 使用Spring进行远程访问与Web服务 17.1. 简介 17.2. 使用RMI暴露服务 17.2.1. 使用 RmiServiceExporter 暴露服务 17.2.2. 在客户端链接服务 17.3. 使用Hessian或者Burlap通过HTTP远程调用服务 17.3.1. 为...

    Spring-Reference_zh_CN(Spring中文参考手册)

    3.8.3. 底层资源的访问 3.8.4. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.9.1. 使用Singleton-helper类 4. 资源 4.1. 简介 4.2. Resource 接口 4.3. 内置 Resource 实现 4.3.1. ...

    spring chm文档

    17. 使用Spring进行远程访问与Web服务 17.1. 简介 17.2. 使用RMI暴露服务 17.2.1. 使用 RmiServiceExporter 暴露服务 17.2.2. 在客户端链接服务 17.3. 使用Hessian或者Burlap通过HTTP远程调用服务 17.3.1. 为...

    Spring攻略(第二版 中文高清版).part2

    6.1 在一般Web应用中访问Spring 209 6.1.1 问题 209 6.1.2 解决方案 210 6.1.3 工作原理 210 6.2 在你的Servlet和过滤器中使用Spring 214 6.2.1 问题 214 6.2.2 解决方案 215 6.2.3 工作原理 215 ...

    Spring中文帮助文档

    3.8.4. 底层资源的访问 3.8.5. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 ...

    Spring API

    3.8.4. 底层资源的访问 3.8.5. ApplicationContext在WEB应用中的实例化 3.9. 粘合代码和可怕的singleton 3.10. 以J2EE RAR文件的形式部署Spring ApplicationContext 3.11. 基于注解(Annotation-based)的配置 ...

    跟开涛学Spring

    1.13 【第四章】 资源 之 4.3 访问Resource ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 1.14 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3 . . . . . . . ...

    Spring Security oauth2

    ###启动资源服务器 运行defender-oauth2-resource模块下ResourceApplication类的main方法。 ###启动客户端服务器 运行defender-oauth2-client模块下ClientApplication类的main方法。 ##访问客户端主...

    Spring攻略(第二版 中文高清版).part1

    6.1 在一般Web应用中访问Spring 209 6.1.1 问题 209 6.1.2 解决方案 210 6.1.3 工作原理 210 6.2 在你的Servlet和过滤器中使用Spring 214 6.2.1 问题 214 6.2.2 解决方案 215 6.2.3 工作原理 215 ...

    SpringBoot页面跳转访问css、js等静态资源引用无效解决.docx

    【SpringBoot页面跳转访问css、js等静态资源引用无效解决】 解释:SpringBoot项目默认访问根目录有三个分别是: /resources :系统默认的根路径 /static :所有静态资源文件如js、css、jpg、html等文件是可以直接...

    SpringSecurity案例以及文档.zip

    Resource Owner,指在Resource Server上拥有资源的一方,需要访问Client,并允许Client从Resource Server获取到自己的信息; Authorization Server,为了保护Resource Owner在Resource Server上的资源,对Client进行...

    Springboot项目打war包docker包找不到resource下静态资源的解决方案

    今天小编就为大家分享一篇关于Springboot项目打war包docker包找不到resource下静态资源的解决方案,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    Spring Cloud与OAuth2整合简介

    Spring Cloud与OAuth2整合简介 1、OAuth2中的角色 2、Authorization Server 3、Resource Server 4、访问资源服务器中的API

    基于Springboot2.3访问本地路径下静态资源的方法(解决报错:Not allowed to load local resource)

    主要介绍了基于Springboot2.3访问本地路径下静态资源的方法(解决报错:Not allowed to load local resource),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着...

    基于Java-的人力资源管理系统的设计与实现.docx

    基于Java-的人力资源管理系统的设计与实现 46 基于Java 的人力资源管理系统的设计与实现 Design and Implementation of a Human Resource Management System Based on Java 内容摘要 本次开发《基于Java的人力资源...

    SpringBoot毕设资源--本人的毕业设计,个人博客网站。用到的后端技术有SpringBoot框架、Spring.zip

    6. 先启动Server的入口类,再启动其他四个模块的入口类,访问 http://127.0.0.1:9091 注意: (1)项目前端采用Vue框架,由于本人对前端不熟,于是写出的前端代码非常一般,并且前端代码在web模块中,并没有真正...

Global site tag (gtag.js) - Google Analytics