在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源、File资源、ClassPath相关资源、服务器相关资源等等很多资源。因此处理这些资源需要使用不同的接口,这就增加了我们系统的复杂性;而且处理这些资源步骤都是类似的(打开资源、读取资源、关闭资源),因此如果能抽象出一个统一的接口来对这些底层资源进行统一访问,是不是很方便,而且使我们系统更加简洁,都是对不同的底层资源使用同一个接口进行访问。
spring提供一个Resource接口来统一这些底层资源一致的访问,而且提供了一些便利的接口,从而能提高开发效率。
Resource接口
Spring的Resource接口代表底层外部资源,提供了对底层外部资源的一致性访问接口。
public interface InputStreamSource{ //每次调用都将返回一个新的资源对应的java.io.InputStream字节流,调用者在使用完毕后必须关闭该资源 InputStream getInputStream() throws IOException; }
public interface Resource extends InputStreamSource{ //返回当前Resouce代表的底层资源是否存在,true表示存在 boolean exists(); //返回当前Resouce代表的底层资源是否可读,true表示可读 boolean isReadable(); //返回当前Resouce代表的底层资源是否已经打开,如果返回true,则只能被读取一次然后关闭以避免内存泄露;常见的Resource实现一般返回false; boolean isOpen(); //如果当前Resouce代表的底层资源能由java.util.URL代表,则返回该URL,否则抛出IOException URL getURL() throws IOException; //如果当前Resouce代表的底层资源能由java.util.URI代表,则返回该URI,否则抛出IOException URI getURI() throws IOException; //如果当前Resouce代表的底层资源能由java.io.File代表,则返回该File,否则抛出IOException File getFile() throws IOException; //返回当前Resouce代表的底层文件资源的长度,一般的值代表的文件资源的长度 long contentLength() throws IOException; //返回当前Resouce代表的底层文件资源的最后修改时间 long lastModified() throws IOException; //用于创建相对于当前Resource代表的底层资源的资源,比如当前Resource代表文件资源“D:/test/”则createRelative("test.txt")将返回代表文件资源“D:/test/test.txt”Resource资源。 Resource createRelative(String relativePath) throws IOException; //返回当前Resource代表的底层文件资源的文件路径,比如File资源:file://d:/test.txt 将返回d:/test.txt,而URL资源http://www.javass.cn将返回“”,因为只返回文件路径 String getFilename(); //返回当前Resource代表的底层资源的描述符,通常就是资源的全路径(实际文件名或实际URL地址) String getDescription(); }
Resource接口提供了足够的抽象,足够满足我们日常使用。而且提供了很多内置Resource实现:ByteArrayResource、InputStreamResource 、FileSystemResource 、UrlResource 、ClassPathResource、ServletContextResource、VfsResource等。
内置Resource实现
ByteArrayResource
ByteArrayResource代表 btye[]数组资源,对于getInputStream操作将返回一个ByteArrayInputStream
import java.io.IOException; import java.io.InputStream; import org.junit.Test; import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; public class ResourceTest { @Test public void testByteArrayResource(){ //1.定义资源 Resource resource = new ByteArrayResource("hello".getBytes()); //2.验证资源 if(resource.exists()){ //3.访问资源 dumpStream(resource); } } private void dumpStream(Resource resource){ InputStream is = null; try { //1.获取资源文件 is = resource.getInputStream(); //2.读取资源文件 byte[] descBytes = new byte[is.available()]; is.read(descBytes); System.out.println(new String(descBytes)); } catch (IOException e) { e.printStackTrace(); } finally{ try { //3.关闭资源 is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
dumpStream方法很抽象定义了访问流的三部曲:打开资源、读取资源、关闭资源
testByteArrayResouce方法也定义了基本步骤:定义资源、验证资源存在、访问资源
ByteArrayResource可多次读取数组资源,即isOpen永远是falseInputStreamResource
InputStreamResource代表Java.io.InputStream字节流,对于个体InputStream操作将直接返回该字节流,因此只能读取一次字节流,即isOpen永远返回true
@Test public void testInputStreamResource(){ ByteArrayInputStream bis = new ByteArrayInputStream("hello2".getBytes()); Resource resource = new InputStreamResource(bis); if (resource.exists()) { dumpStream(resource); } Assert.assertEquals(true, resource.isOpen()); }
FileSystemResource
FileSystemResource代表java.io.File资源,对于getInputStream 操作将返回底层文件的字节流,isOpen将永远返回false,从而表示可多次读取底层文件的字节流
@Test public void testFileSystemResource(){ File file = new File("d:/test.txt"); Resource resource = new FileSystemResource(file); if (resource.exists()) { dumpStream(resource); } Assert.assertEquals(false, resource.isOpen()); }
ClassPathResource
ClassPathResource代表classpath路径的资源,将使用ClassLoader进行加载资源。classpath资源存在于类路径中的文件系统中或jar包里,且isOpen永远返回false,表示可多次读取资源。
ClassPathResource加载资源替代了Class类和ClassLoader类的getResource(String name)和getResouceAsStream(String name) 两个加载类路径资源方法,提供一致的访问方式.public ClassPathResource(String path) : 使用默认的ClassLoader加载“path”类路径资源;
@Test public void testClasspathResourceByDefaultClassLoader() throws IOException{ Resource resource = new ClassPathResource("com/lizhenhua/test3/db.properties"); if (resource.exists()) { dumpStream(resource); } System.out.println("path:" + resource.getFile().getAbsolutePath()); Assert.assertEquals(false, resource.isOpen()); }
public ClassPathResource(String path, ClassLoader classLoader) : 使用指定的ClassLoader加载path类路径资源。使用指定的ClassLoader进行加载资源,将加载指定的ClassLoader类路径上相对于根路径的资源。也就是可以通过这种方式访问jar包中的资源文件
@Test public void testClasspathResourceByClassLoader() throws IOException{ ClassLoader classLoader = TypeComparator.class.getClassLoader(); Resource resource = new ClassPathResource("META-INF/INDEX.LIST",classLoader); if (resource.exists()) { dumpStream(resource); } System.out.println("path:" + resource.getDescription()); Assert.assertEquals(false, resource.isOpen()); }
public ClassPathResource(String path, Class< ?> clazz) : 使用指定的类加载path类路径资源,将加载相对于当前类的路径的资源。
@Test public void testClasspathResourceByClass() throws IOException{ Class clazz = this.getClass(); Resource resource = new ClassPathResource("com/lizhenhua/test3/db.properties",clazz); if (resource.exists()) { dumpStream(resource); } System.out.println("path:" + resource.getDescription()); Assert.assertEquals(false, resource.isOpen()); Resource resource2 = new ClassPathResource("db.properties",clazz); if (resource2.exists()) { dumpStream(resource2); System.out.println(1); } System.out.println("path:" + resource2.getDescription()); Assert.assertEquals(false, resource2.isOpen()); }
UrlResource
UrlResource代表URL资源,用于简化URL资源访问。isOpen永远返回false,可以多次读取资源
支持如下资源访问
http:通过标准的http协议访问web资源,如new UrlResource(“http://地址”);
ftp:通过ftp协议访问资源,如new UrlResource(“ftp://地址”);
file:通过file协议访问本地文件系统资源 如:new UrlResource(file:d:/test.txt)
@Test public void testUrlResource() throws IOException{ Resource resource = new UrlResource("http://www.hao123.com"); if (resource.exists()) { dumpStream(resource); } System.out.println("path:" + resource.getURL().getPath()); Assert.assertEquals(false, resource.isOpen()); }
ServletContextResource
代表web应用资源,用于简化servlet容器的ServletContext接口的getResource操作和getResourceAsStream操作
VfsResource
fsResource代表Jboss 虚拟文件系统资源。
Jboss VFS框架是一个文件系统资源访问的抽象层,它能一致的访问物理文件系统、jar资源、zip资源、war资源等,VFS能把这些资源一致的映射到一个目录上,访问它们就像访问物理文件资源一样,而其实这些资源不存在与物理文件系统。
在示例之前需要准备一些jar包,在此我们使用JBoss VFS3版本,
将Jboss-logging.jar和jboss-vfs.jar两个jar包拷贝到我们项目的lib目录中
@Test public void testVfsResourceForRealFileSystem() throws IOException { //1.创建一个虚拟的文件目录 VirtualFile home = VFS.getChild("/home"); //2.将虚拟目录映射到物理的目录 VFS.mount(home, new RealFileSystem(new File("d:"))); //3.通过虚拟目录获取文件资源 VirtualFile testFile = home.getChild("test.txt"); //4.通过一致的接口访问 Resource resource = new VfsResource(testFile); if(resource.exists()) { dumpStream(resource); } System.out.println("path:" + resource.getFile().getAbsolutePath()); Assert.assertEquals(false, resource.isOpen()); } @Test public void testVfsResourceForJar() throws IOException { //1.首先获取jar包路径 File realFile = new File("lib/org.springframework.beans-3.0.5.RELEASE.jar"); //2.创建一个虚拟的文件目录 VirtualFile home = VFS.getChild("/home2"); //3.将虚拟目录映射到物理的目录 VFS.mountZipExpanded(realFile, home, TempFileProvider.create("tmp", Executors.newScheduledThreadPool(1))); //4.通过虚拟目录获取文件资源 VirtualFile testFile = home.getChild("META-INF/spring.handlers"); Resource resource = new VfsResource(testFile); if(resource.exists()) { dumpStream(resource); } System.out.println("path:" + resource.getFile().getAbsolutePath()); Assert.assertEquals(false, resource.isOpen()); }
访问Resource
ResourceLoader接口
ResourceLoader接口用于返回Resource对象;其实现可以看作是一个生产Resource的工厂类。
public interface ResourceLoader { //用于根据提供的location参数返回相应的Resource对象 Resource getResource(String location); //返回加载这些Resource的ClassLoader。 ClassLoader getClassLoader(); }
Spring提供了一个适用于所有环境的DefaultResourceLoader实现,可以返回ClassPathResource、UrlResource;还提供一个用于web环境的ServletContextResourceLoader,它继承了DefaultResourceLoader的所有功能,又额外提供了获取ServletContextResource的支持。
@Test public void testResourceLoad() { ResourceLoader loader = new DefaultResourceLoader(); Resource resource = loader.getResource("classpath:com/heqing/spring/test1.txt"); //验证返回的是ClassPathResource Assert.assertEquals(ClassPathResource.class, resource.getClass()); Resource resource2 = loader.getResource("file:com/heqing/spring/test1.txt"); //验证返回的是ClassPathResource Assert.assertEquals(UrlResource.class, resource2.getClass()); Resource resource3 = loader.getResource("com/heqing/spring/test1.txt"); //验证返默认可以加载ClasspathResource Assert.assertTrue(resource3 instanceof ClassPathResource); }
ClassPathXmlApplicationContext : 不指定前缀将返回默认的ClassPathResource资源,否则将根据前缀来加载资源;
FileSystemXmlApplicationContext : 指定前缀将返回FileSystemResource,否则将根据前缀来加载资源;
WebApplicationContext : 不指定前缀将返回ServletContextResource,否则将根据前缀来加载资源;
其他 : 不指定前缀根据当前上下文返回Resource实现,否则将根据前缀来加载资源。ResourceLoaderAware接口
ResourceLoaderAware是一个标记接口,用于通过ApplicationContext上下文注入ResourceLoader。
public interface ResourceLoaderAware { void setResourceLoader(ResourceLoader resourceLoader); }
1.先准备测试Bean,我们的测试Bean还简单只需实现ResourceLoaderAware接口,然后通过回调将ResourceLoader保存下来就可以了:
import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.ResourceLoader; public class ResourceBean implements ResourceLoaderAware { private ResourceLoader resourceLoader; @Override public void setResourceLoader(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } public ResourceLoader getResourceLoader() { return resourceLoader; } }
2.配置Bean定义(chapter4/resourceLoaderAware.xml):
<bean class="cn.javass.spring.chapter4.bean.ResourceBean"/>
3.测试
@Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter4/resourceLoaderAware.xml"); ResourceBean resourceBean = ctx.getBean(ResourceBean.class); ResourceLoader loader = resourceBean.getResourceLoader(); Assert.assertTrue(loader instanceof ApplicationContext); }
注入Resource
Spring提供了一个PropertyEditor “ResourceEditor” 用于在注入的字符串和Resource之间进行转换。因此可以使用注入方式注入Resource
ResourceEditor 完全使用ApplicationContext根据注入的路径字符串获取相应的Resource。
1.准备Bean:
public class ResourceBean3 { private Resource resource; public Resource getResource() { return resource; } public void setResource(Resource resource) { this.resource = resource; } }
2.准备配置文件:
<bean id="resourceBean1" class="cn.javass.spring.chapter4.bean.ResourceBean3"> <property name="resource" value="cn/javass/spring/chapter4/test1.properties"/> </bean> <bean id="resourceBean2" class="cn.javass.spring.chapter4.bean.ResourceBean3"> <property name="resource" value="classpath:cn/javass/spring/chapter4/test1.properties"/> </bean>
3.测试:
@Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter4/resourceInject.xml"); ResourceBean3 resourceBean1 = ctx.getBean("resourceBean1", ResourceBean3.class); ResourceBean3 resourceBean2 = ctx.getBean("resourceBean2", ResourceBean3.class); Assert.assertTrue(resourceBean1.getResource() instanceof ClassPathResource); Assert.assertTrue(resourceBean2.getResource() instanceof ClassPathResource); }
路径通配符
使用路径通配符加载Resource
- ? : 匹配一个字符
- * : 匹配零个或多个字符串
- ** : 匹配路径中的零个或多个目录
classpath : 用于加载类路径(包括jar包)中的一个且仅一个资源;对于多个匹配的也只返回一个,所以如果需要多个匹配的请考虑“classpath*:”前缀;
@Test public void testClasspathPrefix() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //只加载一个绝对匹配Resource,且通过ResourceLoader.getResource进行加载 Resource[] resources=resolver.getResources("classpath:META-INF/INDEX.LIST"); Assert.assertEquals(1, resources.length); //只加载一个匹配的Resource,且通过ResourceLoader.getResource进行加载 resources = resolver.getResources("classpath:META-INF/*.LIST"); Assert.assertTrue(resources.length == 1); }
classpath* : 用于加载类路径(包括jar包)中的所有匹配的资源。带通配符的classpath使用“ClassLoader”的“Enumeration getResources(String name)”方法来查找通配符之前的资源,然后通过模式匹配来获取匹配的资源。如“classpath:META-INF/*.LIST”将首先加载通配符之前的目录“META-INF”,然后再遍历路径进行子路径匹配从而获取匹配的资源。
@Test public void testClasspathAsteriskPrefix () throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //将加载多个绝对匹配的所有Resource //将首先通过ClassLoader.getResources("META-INF")加载非模式路径部分 //然后进行遍历模式匹配 Resource[] resources=resolver.getResources("classpath*:META-INF/INDEX.LIST"); Assert.assertTrue(resources.length > 1); //将加载多个模式匹配的Resource resources = resolver.getResources("classpath*:META-INF/*.LIST"); Assert.assertTrue(resources.length > 1); }
file : 加载一个或多个文件系统中的Resource。如“file:D:/*.txt”将返回D盘下的所有txt文件;
无前缀 : 通过ResourceLoader实现加载一个资源。注入Resource数组
<bean id="resourceBean1" class="cn.javass.spring.chapter4.bean.ResourceBean4"> <property name="resources"> <array> <value>cn/javass/spring/chapter4/test1.properties</value> <value>log4j.xml</value> </array> </property> </bean> <bean id="resourceBean2" class="cn.javass.spring.chapter4.bean.ResourceBean4"> <property name="resources" value="classpath*:META-INF/INDEX.LIST"/> </bean> <bean id="resourceBean3" class="cn.javass.spring.chapter4.bean.ResourceBean4"> <property name="resources"> <array> <value>cn/javass/spring/chapter4/test1.properties</value> <value>classpath*:META-INF/INDEX.LIST</value> </array> </property> </bean>
Spring通过ResourceArrayPropertyEditor来进行类型转换的,而它又默认使用“PathMatchingResourcePatternResolver”来进行把路径解析为Resource对象。所有大家只要会使用“PathMatchingResourcePatternResolver”,其它一些实现都是委托给它的,比如AppliacationContext的“getResources”方法等。
AppliacationContext实现对各种Resource的支持
ClassPathXmlApplicationContext
默认将通过classpath进行加载返回
public class ClassPathXmlApplicationContext { //1)通过ResourcePatternResolver实现根据configLocation获取资源 public ClassPathXmlApplicationContext(String configLocation); public ClassPathXmlApplicationContext(String... configLocations); public ClassPathXmlApplicationContext(String[] configLocations, ……); //2)通过直接根据path直接返回ClasspathResource public ClassPathXmlApplicationContext(String path, Class clazz); public ClassPathXmlApplicationContext(String[] paths, Class clazz); public ClassPathXmlApplicationContext(String[] paths, Class clazz, ……); }
第一类构造器是根据提供的配置文件路径使用“ResourcePatternResolver ”的“getResources()”接口通过匹配获取资源;即如“classpath:config.xml”
第二类构造器则是根据提供的路径和clazz来构造ClassResource资源。即采用“public ClassPathResource(String path, Class< ?> clazz)”构造器获取资源。FileSystemXmlApplicationContext
将加载相对于当前工作目录的“configLocation”位置的资源,注意在linux系统上不管“configLocation”是否带“/”,都作为相对路径;而在window系统上如“D:/resourceInject.xml”是绝对路径。因此在除非很必要的情况下,不建议使用该ApplicationContext。
public class FileSystemXmlApplicationContext{ public FileSystemXmlApplicationContext(String configLocation); public FileSystemXmlApplicationContext(String... configLocations,……); }
//linux系统,第一个将相对于当前vm路径进行加载; //第二个则是绝对路径方式加载 ctx.getResource ("chapter4/config.xml"); ctx.getResource ("/root/confg.xml"); //windows系统,第一个将相对于当前vm路径进行加载; //第二个则是绝对路径方式加载 ctx.getResource ("chapter4/config.xml"); ctx.getResource ("d:/chapter4/confg.xml");
因此如果需要加载绝对路径资源最好选择前缀“file”方式,将全部根据绝对路径加载。如在linux系统“ctx.getResource (“file:/root/confg.xml”);”