spring源码解析-资源管理Resource
来源:互联网 发布:淘宝女装模特名字大全 编辑:程序博客网 时间:2024/05/20 06:49
spring提供了常用的资源解析类,我们在平时读取文件过程中也可以直接采用,下面是我画的类图结构
下面我进行一一介绍
/** * java通过class和classLoader获取资源的方式 * @author q */public class GetResourceTest { public static void main(String[] args) { //如果以'/'开头则从classpath路劲下获取 System.out.println(GetResourceTest.class.getResource("")); System.out.println(GetResourceTest.class.getResource("/")); /** * classloader不能以'/'开头,否则为空,classloader是从classpath跟路劲开始获取 */ System.out.println(GetResourceTest.class.getClassLoader().getResource("")); System.out.println(GetResourceTest.class.getClassLoader().getResource("/")); }}
/** * spring 资源获取策略 * @author q * */public class ResourceTest { public static void main(String[] args) throws IOException, URISyntaxException { /** * UriResource,通过网络协议读取文件资源 */ Resource res = new UrlResource("file:///D:/API/JAVA_API_1.7.chm"); res.getFile(); URI uri = new URI("file:///D:/API/JAVA_API_1.7.chm"); System.out.println(uri.getSchemeSpecificPart()); File file = new File("///D:/API/JAVA_API_1.7.chm"); System.out.println(file.exists()); /** * ClassPathResource最后通过this.classLoader.getResource(this.path)来获取资源,所以必须制定包路劲 * @see GetResourceTest */ res = new ClassPathResource("org/bear/bookstore/test/resource/application-orcl1.properties"); System.out.println(res.getFile().exists()); /** * InputStream不做解读 */ //res = new InputStreamResource(null); /** * To be used as placeholder if a {@code Resource} argument is * expected by an API but not necessarily used for actual reading */ //res = new DescriptiveResource("file from native...."); /** * ByteArrayResource不具体解读 */ //res = new ByteArrayResource(null); /** * FileSystemResource直接采用Java的File作为流 */ res = new FileSystemResource("D:/API/JAVA_API_1.7.chm"); System.out.println("FileSystemResource:" + res.getFile().exists()); /** * PathResource直接采用java中@see java.nio.Paths 获取流 */ res = new PathResource("D:/API/JAVA_API_1.7.chm"); System.out.println("PathResource:" + res.getFile().exists()); }}
spring抽象出一个InputStreamResource接口,所有InputStream都可以作为资源供spring使用
public interface InputStreamSource { InputStream getInputStream() throws IOException;}
抽象Resource作为资源判断、操作的接口,提供了一下操作
//是否存在boolean exists();//是否可读boolean isReadable();//是否已经打开boolean isOpen();//获取URL地址URL getURL() throws IOException;//获取资源定位符URI getURI() throws IOException;//获取文件File getFile() throws IOException;//获取资源大小long contentLength() throws IOException;//获取文件最后修改日期long lastModified() throws IOException;//Resource createRelative(String relativePath) throws IOException;//获取文件名String getFilename();//获取资源描述String getDescription();
提供AbstractResource作为Resource接口的实现,注意其中几个方法,均需要其子类实现的,以为每一种方式都不相同,为设计模式策略模式的简单应用
public URL getURL() throws IOException { throw new FileNotFoundException(getDescription() + " cannot be resolved to URL");}public File getFile() throws IOException { throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");}public Resource createRelative(String relativePath) throws IOException { throw new FileNotFoundException("Cannot create a relative resource for " + getDescription());}
剩下的子类就不多解释,资源管理并不复杂,spring提供多种方式获取资源:通过Url获取远程服务器资源,通过classLoader获取classpath下面的资源,通过File、Paths获取本地资源;我们也可以实现自己的资源获取策略供系统使用,有兴趣的可以自己尝试
补充
spring还提供了EncodedResource类,对Resource进行包装,提供通过不同编码方式读取资源文件,采用InputStreamReader读取Resource的流信息
public Reader getReader() throws IOException { if (this.charset != null) { return new InputStreamReader(this.resource.getInputStream(), this.charset); } else if (this.encoding != null) { return new InputStreamReader(this.resource.getInputStream(), this.encoding); } else { return new InputStreamReader(this.resource.getInputStream()); }}
近期准备深度研究spring源码,有工作中没有了解的关于spring的东西,大家可以提出来,我会有针对性研究和写文章,有什么写的不好的希望大家指正,谢谢
0 0
- spring源码解析-资源管理Resource
- Spring源码之Resource加载源码解析(一)
- Spring源码之Resource加载源码解析(二)
- Spring源码之Resource加载源码解析(三)
- spring IoC源码分析 (3)Resource解析
- spring resource源码讲解
- Spring源码——Resource
- 资源管理-Resource management
- Part2:Resource Management/资源管理
- spring security权限资源管理,数据库sql语句源码
- 深入Spring IOC源码之Resource
- Spring源码阅读之Resource接口
- 深入Spring IOC源码之Resource
- spring源码分析:resource资源定位一
- Oracle 资源管理(resource manager)
- Spring源码解析
- spring源码解析
- spring 源码解析
- 解决ScollView嵌套ListView只显示一条的问题
- opencv中的图像亮度变换(图像增强)
- mysql中解决:Error Code: 1044. Access denied for user 'root'@'%' to database
- 欢迎使用CSDN-markdown编辑器
- Redis详细介绍
- spring源码解析-资源管理Resource
- Sql server 创建同义词CREATE SYNONYM介绍
- (转)WebView与JS交互和VedioView的简单使用
- JAVA 各种数值类型最大值和最小值 Int, short, char, long, float,&nbs
- HttpClient数据传输的编码方式
- VOIP听筒与扬声器的切换
- 关于安装MongoDB的过程与错误48 100的解决方法
- windows 下reactnative环境配置
- attemping运行Android模拟器时,32位Linux Android模拟器的二进制文件已被弃用