Spring学习八----------Bean的配置之Resources
© 版权声明:本文为博主原创文章,转载请注明出处
Resources
针对于资源文件的统一接口
-UrlResource:URL对应的资源,根据一个URL地址即可创建
-ClassPathResource:获取类路径下的资源文件
-FileSystemResource:获取文件系统里面的资源
-ServletContextResource:ServletContext封装的资源,用于访问ServletContext环境下的资源(需导入spring-web.jar,需在web环境中测试,本次不测试。)
-InputStreamResource:针对于输入流封装的资源
-ByteArrayResource:针对于字节数组封装的资源
ResourceLoader
spring的IOC容器中,所有的Application context都实现了ResourceLoader这个接口,所有的application context都可以用getResource()方法来获取Resource实例。
前缀:
classpath:从类路径中加载
file:从文件系统中作为url加载
http:作为url加载
(none):依赖于application context,也就是说application context是怎么加载的,它就是怎么加载的
实例
1.项目结构

2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springresources</groupId>
<artifactId>Spring-Resources</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Resources Maven Webapp</name>
<url>http://maven.apache.org</url> <!-- 统一开发所需版本 -->
<properties>
<spring.version>4.3.7.RELEASE</spring.version>
</properties> <dependencies>
<!-- junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies> <build>
<finalName>Spring-Resources</finalName>
</build>
</project>
3.test-resources.txt
测试spring Resource
4.SpringResource.java
package org.spring.resources; import java.io.IOException; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource; public class SpringResource implements ApplicationContextAware { private ApplicationContext context; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } /**
* 通过classpath前缀获取Resource
*
* @throws IOException
*/
public void getClassPathResouce() throws IOException { Resource resource = context.getResource("classpath:test-resources.txt");
System.out.println("ResourceLoader(classpath) filename: " + resource.getFilename());
System.out.println("ResourceLoader(classpath) size: " + resource.contentLength()); } /**
* 通过file前缀获取Resource
*
* @throws IOException
*/
public void getFileResouce() throws IOException { Resource resource = context.getResource("file:E:\\code\\Spring-Resources\\src\\main\\resources\\test-resources.txt");
System.out.println("ResourceLoader(file) filename: " + resource.getFilename());
System.out.println("ResourceLoader(file) size: " + resource.contentLength()); } /**
* 通过url前缀获取Resource
*
* @throws IOException
*/
public void getUrlResouce() throws IOException { Resource resource = context.getResource("url:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/");
System.out.println("ResourceLoader(url) filename: " + resource.getFilename());
System.out.println("ResourceLoader(url) size: " + resource.contentLength()); } /**
* 无前缀获取Resource
*
* 跟application context有关,本代码中的application context是根据classpath获取的,所以该方法中也是根据classpath前缀获取的Resource
*
* @throws IOException
*/
public void getResouce() throws IOException { Resource resource = context.getResource("test-resources.txt");
System.out.println("ResourceLoader(none) filename: " + resource.getFilename());
System.out.println("ResourceLoader(none) size: " + resource.contentLength()); } }
5.spring-resources.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="springResource" class="org.spring.resources.SpringResource"/> </beans>
6.JunitTestBase.java
package org.spring.resources.test; import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils; /**
* Junit基类
*
*/
public class JunitTestBase { private ClassPathXmlApplicationContext context;
private String springXmlPath; /**
* 无参构造器,使用此构造器则使用默认spring配置文件
*/
public JunitTestBase() { } /**
* 含参构造器,初始化spring配置文件路径
*
* @param springXmlPath
* spring配置文件路径
*/
public JunitTestBase(String springXmlPath) {
super();
this.springXmlPath = springXmlPath;
} /**
* 加载spring配置文件到容器中,并启动容器
* 在@Test方法执行之前执行
*/
@Before
public void before() { if(StringUtils.isEmpty(springXmlPath)) {//默认spring配置文件
springXmlPath = "classpath:spring-*.xml";
}
//加载spring配置文件到spring容器中
context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+"));
//启动spring容器,并将启动信号扩散器该容器下的所有组件中
context.start(); } /**
* 销毁Spring容器
* 在@Test方法执行之后执行
*/
@After
public void after() { if(context != null){
context.destroy();
} } /**
* 通过bean Id获取对象
*
* @param beanId
* bean id
* @return
*/
public Object getBean(String beanId) { return context.getBean(beanId); } }
7.TestSpringResources.java
package org.spring.resources.test; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.spring.resources.SpringResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.UrlResource; @RunWith(BlockJUnit4ClassRunner.class)//默认执行类,可不写
public class TestSpringResources extends JunitTestBase { //构造器初始化spring配置文件
public TestSpringResources() { super("classpath:spring-resources.xml"); } /**
* 测试UrlResource
*
* @throws IOException
*/
@Test
public void testUrlResources() throws IOException { UrlResource resource = new UrlResource("http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/");
System.out.println("UrlResource filename:" + resource.getFilename());
System.out.println("UrlResource size:" + resource.contentLength()); } /**
* 测试ClassPathResource
*
* @throws IOException
*/
@Test
public void testClassPathResource() throws IOException { ClassPathResource resource = new ClassPathResource("test-resources.txt");
System.out.println("ClassPathResource filename:" + resource.getFilename());
System.out.println("ClassPathResource size:" + resource.contentLength()); } /**
* 测试FileSystemResource
*
* @throws IOException
*/
@Test
public void testFileSystemResource() throws IOException { FileSystemResource resource = new FileSystemResource("E:\\code\\Spring-Resources\\src\\main\\resources\\test-resources.txt");
System.out.println("FileSystemResource filename:" + resource.getFilename());
System.out.println("FileSystemResource size:" + resource.contentLength()); } /**
* 测试InputStreamResource
*
* @throws IOException
*/
@Test
public void testInputStreamResource() throws IOException { InputStream is = new FileInputStream("E:\\code\\Spring-Resources\\src\\main\\resources\\test-resources.txt");
InputStreamResource resource = new InputStreamResource(is);
System.out.println("InputStreamResource filename:" + resource.getFilename());
System.out.println("InputStreamResource size:" + resource.contentLength()); } /**
* 测试ByteArrayResource
*
* @throws IOException
*/
@Test
public void testByteArrayResource() throws IOException { ByteArrayResource resource = new ByteArrayResource("Test Resource".getBytes());
System.out.println("ByteArrayResource filename:" + resource.getFilename());
System.out.println("ByteArrayResource size:" + resource.contentLength()); } /**
* 测试ResourceLoader-classpath前缀
*
* @throws IOException
*/
@Test
public void testResourceLoaderClassPath() throws IOException { SpringResource springResource = (SpringResource) super.getBean("springResource");
springResource.getClassPathResouce(); } /**
* 测试ResourceLoader-file前缀
*
* @throws IOException
*/
@Test
public void testResourceLoaderFile() throws IOException { SpringResource springResource = (SpringResource) super.getBean("springResource");
springResource.getFileResouce(); } /**
* 测试ResourceLoader-url前缀
*
* @throws IOException
*/
@Test
public void testResourceLoaderUrl() throws IOException { SpringResource springResource = (SpringResource) super.getBean("springResource");
springResource.getUrlResouce(); } /**
* 测试ResourceLoader-无前缀
*
* @throws IOException
*/
@Test
public void testResourceLoader() throws IOException { SpringResource springResource = (SpringResource) super.getBean("springResource");
springResource.getResouce(); } }
8.效果预览
8.1 执行testUrlResources方法

8.2 执行testClassPathResource方法 
8.3 执行testFileSystemResource方法

8.4 执行testInputStreamResource方法

8.5 执行testByteArrayResource方法

8.6 执行testResourceLoaderClassPath方法

8.7 执行testResourceLoaderFile方法

8.8 执行testResourceLoaderUrl方法

8.9 执行testResourceLoader方法

参考:http://www.imooc.com/video/3758
Spring学习八----------Bean的配置之Resources的更多相关文章
- Spring学习七----------Bean的配置之自动装配
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...
- Spring学习十一----------Bean的配置之基于Java的容器注解@Bean
© 版权声明:本文为博主原创文章,转载请注明出处 @Bean -@Bean标识一个用于配置和初始化一个由SpringIOC容器管理的新对象的方法,类似于XML配置文件的<bean/> -可 ...
- Spring学习十----------Bean的配置之Autowired注解实现
© 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ...
- Spring学习五----------Bean的配置之Bean的生命周期
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的生命周期 1.定义 2.初始化 3.使用 4.销毁 初始化和销毁的三种方式 1.实现org.springframework.beans.fa ...
- Spring学习九----------Bean的配置之Bean的定义及作用域的注解实现
© 版权声明:本文为博主原创文章,转载请注明出处 Spring Bean常用注解 @Component:通常注解,可用于任何Bean @Repository:通常用于注解DAO层,即持久层 @Serv ...
- spring学习六----------Bean的配置之Aware接口
© 版权声明:本文为博主原创文章,转载请注明出处 Aware Spring提供了一些以Aware结尾的接口,实现了Aware接口的bean在被初始化后,可以获取相应的资源 通过Aware接口,可以对S ...
- Spring学习四----------Bean的配置之Bean的配置项及作用域
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的作用域(每个作用域都是在同一个Bean容器中) 1.singleton:单例,指一个Bean容器中只存在一份(默认) 2.prototype ...
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- 跟着柴毛毛学Spring(3)——简化Bean的配置
通过前面的学习.我们会感觉到对于一个有较多Bean的大项目,Spring的配置会比較复杂. 那么接下来我们就介绍怎样简化Spring的配置. 简化Spring的配置主要分为两类: 1. 自己主动装配 ...
随机推荐
- Jobedin外企招聘网:6个要点不能不知道(转载)
又到一年跳槽季,无数职场人士都在蠢蠢欲动,在工作之余,寻找更好的工作. Jobedin外企招聘网,特别提醒:年末跳槽,一定要在拿到新公司的Offer后,才能向现公司提出辞职.不然新工作没找到,却可能丢 ...
- Codeforces 911F Tree Destruction(贪心 && 树的直径)
题目链接 Tree Destructi 题意 给定一棵树,每次可以选定树上的两个叶子,并删去其中的一个.答案每次加上两个选定的叶子之间的距离. 求最后答案的最大值. 首先求出树的某一条直径,令其端 ...
- 从壹开始 [ Ids4实战 ] 之四 ║ 用户数据管理 & 前后端授权联调
前言 哈喽~~~ 大家周一好!夏天到了,大家舒服了没有,熟话说,打败你的不是天真,是天真热!
- 机器学习(4):数据分析的工具-pandas的使用
前面几节说一些沉闷的概念,你若看了估计已经心生厌倦,我也是.所以,找到了一个理由来说一个有兴趣的话题,就是数据分析.是什么理由呢?就是,机器学习的处理过程中,数据分析是经常出现的操作.就算机器对大量样 ...
- codevs——1220 数字三角形(棋盘DP)
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 如图所示的数字三角形,从顶部出发,在每一结点可以选择向左走或 ...
- Network | TCP
Transmission Control Protocol, TCP是一种面向连接的.可靠的.基于字节流的传输层通信协议. 应用层向TCP层发送用于网间传输的.用8位字节表示的数据流,然后TCP把数据 ...
- windows 下使用github客户端报错:Failed to publish this branch
在windows系统下使用github客户端同步的时候报错“Failed to publish this branch”,查找原因,发现结果是安装vscode的时候没有检查到git,然后安装git后库 ...
- CMake的应用(在vision studio2008中去掉 ALL_BUILD 和 ZERO_CHECK)
from http://blog.csdn.net/jtop0/article/details/6167432 一般由CMake是 跨平台软件开发和维护过程的 工程构建工具.“在每个系统构建你的工程 ...
- ubuntu下C/C++编程起步
1. 安装VMware虚拟机软件 2. 在VMware中安装linux系统,这里安装的是Ubuntu.(用 VMware 安装 Ubuntu 12.04详细过程图解) 3. ubuntu有自带文本编辑 ...
- 深入Java----集合----BitSet
BitSet类 大小可动态改变, 取值为true或false的位集合.用于表示一组布尔标志. java中有三种移位运算符 << : 左移运算符,num <&l ...