Spring 中的 ResourceLoader实例详解
作者:杨过姑父
Spring框架提供了ResourceLoader接口,用于加载资源文件,DefaultResourceLoader是其基本实现,只能加载单个资源,而ResourcePatternResolver继承自ResourceLoader,增加了按模式加载多个资源的能力,感兴趣的朋友一起看看吧
ResourceLoader 是用来将所需要的资源加载 并封装成 Resource对象
public void printStream(InputStream inputStream) throws IOException { Reader reader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(reader); String line = null; while ( (line = bufferedReader.readLine()) != null) { System.out.println(line); } }
1.DefaultResourceLoader
DefaultResourceLoader是 ResourceLoader 一个默认的实现,只能加载单个的资源
@Test public void getResource() throws IOException { ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("beans.xml"); InputStream inputStream = resource.getInputStream(); printStream(inputStream); } @Test public void getResourceByPath() throws IOException { ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("/com/fll/TestCase.class"); InputStream inputStream = resource.getInputStream(); printStream(inputStream); }
2.ResourcePatternResolver
ResourcePatternResolver也是继承了ResourceLoader,但是ResourcePatternResolver又定义了一个加载多个资源的方法,就是按照一个Pattern加载所有符合的资源
PathMatchingResourcePatternResolver一个按照ant风格的路径进行资源匹配加载的实现类
@Test public void getResources() throws Exception { ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resourcePatternResolver.getResources("/**"); Arrays.stream(resources).forEach( resource -> { if(resource.getFilename().equals("beans.xml")){ try { InputStream inputStream = resource.getInputStream(); printStream(inputStream); } catch (IOException e) { throw new RuntimeException(e); } }else { System.out.println(resource.getFilename()); } } ); }
/** 代表加载classpath下的所有资源
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean name="person" class="com.fll.start.Person"> <property name = "name" value = "zhangsan"/> <property name = "age" value = "20"/> </bean> </beans> TestCase.class AnnotationContainerStartProcess.class Car.class InputStreamPrinter.class Person.class XmlContainerStartProcess.class DefaultResourceLoaderTest.class InputResourceTest.class ResourcePatternResolverTest.class SpringStringUtilsTest.class resource_loader start fll com
可以看到确实匹配到了所有的资源,包括目录
到此这篇关于Spring 中的 ResourceLoader的文章就介绍到这了,更多相关Spring ResourceLoader内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!