Spring中Resource(资源)的获取
来源:互联网 发布:windows sdk 版本8.1 编辑:程序博客网 时间:2024/05/23 12:48
Spring中获取资源的方式一共有以下四种:
- 通过Resource接口获取资源
- 通过ResourceLoader接口获取资源
- 通过ApplicationContext获取资源
- 将resource注入到bean中的方式获取资源
下面是对每种方式的详细讲解 :
1.通过Resource接口获取资源
Resource接口的实现类有:
Resource接口继承了InputStreamSource
接口,InputStreamSource
接口中有一个方法:getInputStream()
,所以汇总起来,Resource接口中共有以下方法:
演示代码:
public class ResourceTest { /**使用ClassPathResource获取资源**/ @Test public void TestClassPath() throws IOException{ Resource resource = new ClassPathResource("test.txt"); //判断文件是否存在: if (resource.exists()) { System.out.println("文件存在"); } //判断资源文件是否可读 if (resource.isReadable()) { System.out.println("文件可读"); } //判断当前Resource代表的底层资源是否已经打开 if (resource.isOpen()) { System.out.println("资源文件已打开"); } System.out.println(resource.getURL());//获取资源所在的URL System.out.println(resource.getURI());//获取资源所在的URI resource.getFile();//返回当前资源对应的File。 System.out.println(resource.contentLength());//输出内容长度 System.out.println(resource.lastModified());//返回当前Resource代表的底层资源的最后修改时间。 resource.createRelative("MyFile");//根据资源的相对路径创建新资源。[默认不支持创建相对路径资源] System.out.println(resource.getFilename());//获取资源文件名 System.out.println(resource.getDescription()); //获取当前资源代表的输入流 if (resource.isReadable()) { InputStream is = resource.getInputStream(); System.out.println(is); is.close(); } } /**使用FileSystemResource获取资源**/ @Test public void TestFileSystem() throws IOException { Resource resource = new FileSystemResource("D:\\test.txt"); System.out.println(resource.getFilename()); } /**使用UrlResource获取资源**/ @Test public void TestUrl() throws MalformedURLException{ Resource resource = new UrlResource("http://docs.spring.io/spring/docs/4.0.0.M1/spring-framework-reference/pdf/spring-framework-reference.pdf"); System.out.println(resource.getFilename()); } /**使用ByteArrayResource获取字节数组封装的资源**/ @Test public void testByteArray() throws IOException { ByteArrayResource resource = new ByteArrayResource("Hello".getBytes()); System.out.println(resource.getInputStream()); } /**使用InputStreamResource获取输入流封装的资源。针对于输入流的Resource,其getInputStream()方法只能被调用一次。**/ @Test public void testInputStream() throws Exception { InputStream is = new FileInputStream("D\\test.txt"); InputStreamResource resource = new InputStreamResource(is); //对于InputStreamResource而言,其getInputStream()方法只能调用一次,继续调用将抛出异常。 InputStream is2 = resource.getInputStream(); //返回的就是构件时的那个InputStream System.out.println(is2); is.close(); } }
2.通过ResourceLoader接口获取资源
Spring框架为了更方便的获取资源,尽量弱化程序员对各个Resource接口的实现类的感知,定义了另一个ResourceLoader
接口。该接口的getResource(String location)
方法可以用来获取资源。它的DefaultResourceLoader实现类可以适用于所有的环境,可以返回ClassPathResource、UrlResource等。
ResourceLoader在进行加载资源时需要使用前缀来指定需要加载:“classpath:path”表示返回ClasspathResource,“http://path”和“file:path”表示返回UrlResource资源,如果不加前缀则需要根据当前上下文来决定,DefaultResourceLoader默认实现是加载classpath资源。
示例代码:
@Test public void testResourceLoader() { ResourceLoader loader = new DefaultResourceLoader(); Resource resource = loader.getResource("http://www.baidu.com"); System.out.println(resource instanceof UrlResource); //true resource = loader.getResource("classpath:test.txt"); System.out.println(resource instanceof ClassPathResource); //true resource = loader.getResource("test.txt"); System.out.println(resource instanceof ClassPathResource); //true }
3.通过ApplicationContext获取资源
事实上,所有ApplicationContext实例都实现了ResourceLoader接口,因此我们在使用Spring容器时,可以不去过于计较底层Resource的实现,也不需要自己创建Resource实现类,而是直接使用applicationContext.getResource(),获取到bean容器本身的Resource,进而取到相关的资源信息。
示例代码:
首先创建一个MyResource类:这个类实现了ApplicationContextAware接口来获取上下文对象
public class MyResource implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public void resource() throws IOException { Resource resource = applicationContext.getResource("file:D:\\test.txt"); System.out.println(resource.getFilename()); System.out.println(resource.contentLength()); }}
配置xml文件:
<bean id="myResource" class="com.spring.test.MyResource"></bean>
测试类:
public class App { public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-resource.xml"); MyResource myResource = (MyResource) context.getBean("myResource"); try { myResource.resource(); } catch (IOException e) { e.printStackTrace(); } }}
测试结果:
4.将resource注入到bean中的方式获取资源
通过依赖注入的方式把Resource注入到bean中。
示例代码:
public class MyResource { private Resource resource; public void setResource(Resource resource) { this.resource = resource; } public void resource() throws IOException { System.out.println(resource.getFilename()); System.out.println(resource.contentLength()); }}
配置spring-resource xml文件:
<bean id="myResource" class="com.spring.test.MyResource"> <property name="resource"> <value>classpath:test.txt</value> </property> </bean>
测试类:
public class App { public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-resource.xml"); MyResource myResource = (MyResource) context.getBean("myResource"); try { myResource.resource(); } catch (IOException e) { e.printStackTrace(); } }}
测试结果为:
参考资料:http://elim.iteye.com/blog/2016305
- Spring中Resource(资源)的获取
- Spring Resource接口获取资源
- Spring中Resource对象的获取
- 通过Spring Resource接口获取资源(6)
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- 通过Spring Resource接口获取资源
- Spring讲解-----------资源(resource)
- Java中资源(resource)获取 Class.getResource() & ClassLoader.getResource()
- 各种获取Resource资源的方法
- spring resource资源
- Meaven jar包服务器搭建(windows 系统)
- 商标申请号查询我们应该如何查询商标
- .net面试题,各大企业常见的.net面试题之四
- Linux系统运维工程该具备哪些素质
- BZOJ 2301 Problem b (莫比乌斯反演)
- Spring中Resource(资源)的获取
- MySQL笔记一
- NodeJs(一)
- Java:编写一个程序输出斐波那契数列中的前20个数
- JAVA 代理模式(Proxy)的原理和实现
- JVM学习笔记(一)—基本结构
- 多线程中的信号机制--sigwait()函数
- ThreadLocal源码解析
- 数论模板