Spring 中的 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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
java 中类似js encodeURIComponent 函数的实现案例
这篇文章主要介绍了java 中类似js encodeURIComponent 函数的实现案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-10-10
使用Java将字符串在ISO-8859-1和UTF-8之间相互转换
大家都知道在一些情况下,我们需要特殊的编码格式,如:UTF-8,但是系统默认的编码为ISO-8859-1,遇到这个问题,该如何对字符串进行两个编码的转换呢,下面小编给大家分享下java中如何在ISO-8859-1和UTF-8之间相互转换,感兴趣的朋友一起看看吧2021-12-12
SpringBoot+thymeleaf+Echarts+Mysql 实现数据可视化读取的示例
本文主要介绍了SpringBoot+thymeleaf+Echarts+Mysql 实现数据可视化读取的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-04-04


最新评论