`

Spring.Resource

 
阅读更多

 



 

 

ByteArrayResource:通过byte 数组构建Resource

public class ByteArrayResource extends AbstractResource {

	private final byte[] byteArray;

	private final String description;

	/**
	 * This implementation returns a ByteArrayInputStream for the
	 * underlying byte array.
	 * @see java.io.ByteArrayInputStream
	 */
	public InputStream getInputStream() throws IOException {
		return new ByteArrayInputStream(this.byteArray);
	}

}

 FileSystemResource:文件资源,可读写

public class FileSystemResource extends AbstractResource implements WritableResource {

	private final File file;

	private final String path;

	/**
	 * This implementation opens a FileInputStream for the underlying file.
	 * @see java.io.FileInputStream
	 */
	public InputStream getInputStream() throws IOException {
		return new FileInputStream(this.file);
	}

	/**
	 * This implementation opens a FileOutputStream for the underlying file.
         * WritableResouce接口方法
	 * @see java.io.FileOutputStream
	 */
	public OutputStream getOutputStream() throws IOException {
		return new FileOutputStream(this.file);
	}
	/**
	 * Create a new FileSystemResource from a file path.
	 * <p>Note: When building relative resources via {@link #createRelative},
	 * it makes a difference whether the specified resource base path here
	 * ends with a slash or not. In the case of "C:/dir1/", relative paths
	 * will be built underneath that root: e.g. relative path "dir2" ->
	 * "C:/dir1/dir2". In the case of "C:/dir1", relative paths will apply
	 * at the same directory level: relative path "dir2" -> "C:/dir2".
	 * @param path a file path
	 */
	public FileSystemResource(String path) {
		Assert.notNull(path, "Path must not be null");
		this.file = new File(path);
		this.path = StringUtils.cleanPath(path);//路径处理:如前缀,..等
	}
}

 ClassPathResource:

 通过给定的classLoader或者class加载资源文件

public class ClassPathResource extends AbstractFileResolvingResource {

	private final String path;

	private ClassLoader classLoader;

	private Class<?> clazz;

public ClassPathResource(String path, ClassLoader classLoader) {
		Assert.notNull(path, "Path must not be null");
		String pathToUse = StringUtils.cleanPath(path);
		if (pathToUse.startsWith("/")) {
			pathToUse = pathToUse.substring(1);
		}
		this.path = pathToUse;
               //classLoader为空,则初始化当前线程的classLoader Thread.currentThread().getContextClassLoader();
		this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
	}
}

    资源文件方式:

    

	/**
	 * This implementation opens an InputStream for the given class path resource.
	 * @see java.lang.ClassLoader#getResourceAsStream(String)
	 * @see java.lang.Class#getResourceAsStream(String)
	 */
	public InputStream getInputStream() throws IOException {
		InputStream is;
		if (this.clazz != null) {
			is = this.clazz.getResourceAsStream(this.path);
		}
		else {
			is = this.classLoader.getResourceAsStream(this.path);
		}
		if (is == null) {
			throw new FileNotFoundException(
					getDescription() + " cannot be opened because it does not exist");
		}
		return is;
	}

 

  • 大小: 128.8 KB
分享到:
评论

相关推荐

    spring-web-2.5.jar

    org.springframework.web.context.support.ServletContextResource.class org.springframework.web.context.support.ServletContextResourceLoader.class org.springframework.web.context.support....

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    Praise for the Third Edition of Spring in Action Preface Acknowledgments About this Book 1. Core Spring Chapter 1. Springing into action 1.1. Simplifying Java development 1.1.1. Unleashing the power ...

    java javax.annotation.Resource注解的详解

    主要介绍了javax.annotation.Resource注解的详解的相关资料,需要的朋友可以参考下

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...

    maven-shade-plugin-3.1.0.jar

    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt; &lt;resource&gt;META-INF/spring.handlers&lt;/resource&gt; implementation="org.apache.maven.plugins.shade.resource....

    4Spring自动装配——annotation resource方式

    NULL 博文链接:https://garrincha.iteye.com/blog/2109688

    Spring入门笔记.md

    ## Spring入门学习 首先认识下Spring的结构 ![架构图](http://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/image001.gif) 然后我们皆可以写我们的demo了 ### 我们的Bean类 对于bean的理解,希望...

    webx3框架指南PDF教程附学习Demo

    • 扩展性 —— Webx 3.0对Spring做了扩展,使Spring Bean不再是“bean”,而是升级成“组件”。一个组件可以扩展另一个组件,也可以被其它组件扩展。这种机制造就了Webx的非常好的扩展性,且比未经扩展的Spring更易...

    Spring 3.x 中文开发手册.pdf

    &lt;import resource="${JAVA_HOME}/com/bank/service/${env}-config.xml"/&gt; 5、xml的什么,不感兴趣 6、hibernate4支持,不感兴趣 7、spring测试框架和2,3,4的结合 8、spring配置文件中namespace的事情,不感兴趣 9...

    spring-resource-handling, Spring Framework 4.1资源处理示例.zip

    spring-resource-handling, Spring Framework 4.1资源处理示例 spring-资源处理 这里应用程序演示 Spring Framework 4.1中的新资源处理功能。 它最初是为在 4.1中讨论资源而开发的,在 SpringOne2GX 2014中讨论。...

    spring-resource

    利用spring resource读取配置文件。

    跟开涛学Spring

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

    springAOP demo 带错误解决文档

    Offending resource: class path resource [beans.xml] at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70) 由于缺少依赖包 aspectjweaver-1.6.12...

    Spring AOP配置源码

    @Component("userService")等价于在spring配置文件中定义一个&lt;bean id="userService"/&gt; @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....

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

    2. resource文件夹中有sql文件、Nginx服务器、Ftp服务器、和部分图片。FTP文件服务器用作上传图片,Nginx用作反向代理,如果嫌麻烦,文件上传可以只上传到本地,只要修改代码即可 3. 将ftpfile文件夹放到D:/blogs/ ...

    spring-framework-3.0.5.RELEASE-dependencies-1

    javax.resource javax.servlet javax.transaction javax.validation javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache ...

    spring使用resource注解的demo

    spring使用resource注解的demo,依赖的库是spring core,建议通过myeclise添加。

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring.doc

    3.6.3 @Resource 27 3.6.4 @PostConstruct 28 3.6.5 @PreDestroy 28 注解注入拓展: 28 3.7扫描注入 30 注解扫描拓展: 32 Mvc用注解写: 34 Spring容器IOC和di的整个启动过程: 38 3.8 spring中的继承 38 拓展...

    1.@Resource是按名称进行注入的,属于java自带的。@Autowired是按类型进行注入的,属于Spring。.pdf

    1.@Resource是按名称进行注入的,属于java自带的。@Autowired是按类型进行注入的,属于Spring。

Global site tag (gtag.js) - Google Analytics