官方网站数据如何做脚注,随州网站制作价格,松江品划做网站公司,企业主页设计撸一撸Spring Framework-IoC系列文章目录
IoC概述
BeanFactory
ApplicationContext
BeanDefinition
BeanWrapper
资源管理利器(Resource与ResourceLoader)
配置管理(Environment与PropertySource)
实现通用功能的利器(PostProcessor机制) 我们经常需要读取外部资源到应… 撸一撸Spring Framework-IoC系列文章目录
IoC概述
BeanFactory
ApplicationContext
BeanDefinition
BeanWrapper
资源管理利器(Resource与ResourceLoader)
配置管理(Environment与PropertySource)
实现通用功能的利器(PostProcessor机制) 我们经常需要读取外部资源到应用中比如文本文件、properties文件、图片文件等。这些资源可能位于不同的位置比如文件系统、classpath下的资源、或者远程服务器上的资源。通常我们需要通过不同的API分别加载不同类型路径的资源有诸多不便之处 Spring提供了Resource系列接口解决上述问题API非常友好、强大 Resource是对诸如文件系统资源、classpath资源、URL资源等各种资源的抽象 ResourceLoader提供了统一的加载Resource的方法它通过资源路径前缀自动匹配相应的资源类型为开发者屏蔽了使用不同Resource实现的差异 ResourcePatternResolver比ResourceLoader更强大支持以通配符的方式加载所有满足条件的资源 Spring内部就通过Resource系列接口进行资源加载比如我们定义的xml文件、properties文件、ComponentScan扫描的java类文件等都是通过这套接口进行加载的
除此之外Resource相关接口可以脱离Spring独立使用我们可以通过如下方式加载不同类型资源
//资源加载器(ResourceLoader接口的默认实现类)
ResourceLoader resourceLoadernew DefaultResourceLoader();
//通过指定不同前缀的资源路径加载不同类型的资源
//通过文件系统绝对路径加载资源
Resource resource resourceLoader.getResource(file:D:/code/spring/src/main/resources/demo.xml);
//通过相对于当前项目根目录的相对路径加载资源
Resource resource resourceLoader.getResource(file:src/main/resources/demo.xml);
//加载classpath下的资源
Resource resource resourceLoader.getResource(classpath:demo.xml);
Resource resource resourceLoader.getResource(classpath:com/example/spring/ResourceLoaderDemo.class);
//通过https url加载CSDN上的一篇博客资源
Resource resource resourceLoader.getResource(https://blog.csdn.net/wb_snail/article/details/108134550);
(tips按通配符加载一组资源的方式下文会有说明)
拿到Resource后可以调用Resource#getInputStream获取资源输入流读取其内容
InputStream inputStreamresource.getInputStream();
BufferedReader reader new BufferedReader(new InputStreamReader(inputStream));
while (true) {String line reader.readLine();if (line null)break; System.out.println(line);
}
reader.close();
如果你想通过与Spring集成的方式使用ResourceLoader可以这样操作 1、实现ResourceLoaderAware接口Spring容器启动过程中回调注入ResourceLoader实例 2、Autowired ResourceLoader resourceLoader(Spring容器启动过程中会向BeanFactory中注入一些特殊的对象(包括ResourceLoader对象 )特殊是因为它们没有被定义为bean它们是spring内部的组件spring允许我们通过Autowire来使用它们) 3、使用ApplicationContextApplicationContext继承于ResourceLoader实际上通过前两种方式拿到的ResourceLoader对象就是ApplicationContext(默认情况下由于AbstractApplicationContext继承于DefaultResourceLoader使用ApplicationContext进行资源加载时最终还是会由DefaultResourceLoader执行) Resource接口比较核心的方法有 getInputStream():继承于InputStreamSource接口返回资源对应的输入流用于读取资源 exists():返回资源是否存在的标识 getFile():如果资源存在于文件系统中返回对应的文件对象否则抛出FileNotFoundException(比如ByteArrayResource这种只在内存中存在的资源) getURL():返回资源对应的URL(java.net.URL)URL是资源定位符上述例子中的file:D:/demo.xml、classpath:demo.xml、https://blog.csdn.net/wb_snail/article/details/108134550都是URL的String表示形式 Resource继承体系如下(体系比较大这里只挑选了一些比较常见的Resource实现) InputStreamSourceResource的父接口只有一个getInputStream()方法 WritableResource可写资源它的方法getOutputStream()可以返回资源的输出流 FileSystemResource文件系统资源可通过File对象、文件系统绝对路径、Path对象(如Paths.get(D:/demo.xml))来构建(tipsSpring在处理ComponentScan定义的包路径下的class文件时会将它们加载为FileSystemResource) ClassPathResourceclasspath下的资源(tipsSpring在处理PropertyResource、PropertyResources时相关配置文件会被加载为ClassPathResource) UrlResource引用了一个java.net.URL对象可以访问任何可以用URL表示的资源(file、https、ftp等资源) ServletContextResourceweb应用资源资源路径以相对于web应用根目录的路径表示比如new ServletContextResource(/WEB-INF/demo.xml) ByteArrayResource通过一个二进制数组创建的资源比如new ByteArrayResource(new String(hello).getBytes(StandardCharsets.UTF_8)) Resource几乎可以表示任何类型的底层资源除了Spring已经实现的多种资源类型外你还可以实现自己的Resource(比如DB中的资源)你可以向ResourceLoader中注册一个ProtocolResolver然后就可以使用ResourceLoader以与其他类型资源无差别的方式加载你的资源。相关源码如下
public class CustomResource {public static void main(String[] args) {DefaultResourceLoader resourceLoadernew DefaultResourceLoader();//注册自定义ProtocolResolverresourceLoader.addProtocolResolver(new DbProtocolResolver());//通过db:为前缀的路径加载db中的资源Resource dbResourceresourceLoader.getResource(db:dataSource_fileDB/table_file/column_content);}
}//自定义Resource
public class DbResource extends AbstractResource {private final String path;...
}/*** 实现ProtocolResolver针对前缀为db:的url返回DBResource*/
public class DbProtocolResolver implements ProtocolResolver {private static final String DB_URL_PREFIXdb:;Nullablepublic Resource resolve(String location, ResourceLoader resourceLoader){if(location.startsWith(DB_URL_PREFIX)){return new DbResource(location.substring(DB_URL_PREFIX.length()));}return null;}
}//以下是DefaultResourceLoader中相关部分的源码
public class DefaultResourceLoader implements ResourceLoader {//一组ProtocolResolver实例private final SetProtocolResolver protocolResolvers new LinkedHashSet(4);public DefaultResourceLoader() {}//调用该方法注册你自定义的ProtocolResolverpublic void addProtocolResolver(ProtocolResolver resolver) {Assert.notNull(resolver, ProtocolResolver must not be null);this.protocolResolvers.add(resolver);}Overridepublic Resource getResource(String location) {Assert.notNull(location, Location must not be null);//加载资源时优先通过你注册的ProtocolResolver加载资源for (ProtocolResolver protocolResolver : getProtocolResolvers()) {Resource resource protocolResolver.resolve(location, this);if (resource ! null) {return resource;}}if (location.startsWith(/)) {return getResourceByPath(location);}else if (location.startsWith(CLASSPATH_URL_PREFIX)) {return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());}else {try {// Try to parse the location as a URL...URL url new URL(location);return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));}catch (MalformedURLException ex) {// No URL - resolve as resource path.return getResourceByPath(location);}}}
}
ResourceLoader仅支持匹配单个资源其扩展接口ResourcePatternResolver支持以通配符的方式加载所有满足条件的资源比如classpath*:META-INF/spring.handlers、classpath:META-INF/*.properties、/WEB-INF/*-context.xml、file:D:/resources/*.properties
注意classpath:和classpath*:的区别后者会把classpath下所有jar包也作为查找目标ComponentScan可以扫描jar包下的Component正是利用了这个特性Spring通过查找spring.handlers文件实现SPI也是一样道理
ResourcePatternResolver支持三种通配符 *匹配资源路径中的任意字符 ?匹配资源路径中的单个字符 如*.xml可以匹配到a.xml、ab.xml而?.xml只能匹配到a.xml **匹配任意层级比如mapper/**/*Mapper.xml可以匹配到mapper/RoleMapper.xml、mapper/order/OrderMapper.xml、mapper/order/goods/GoodsMapper.xml Spring Framework中ResourcePatternResolver的唯一有效实现是PathMatchingResourcePatternResolverApplicationContext也继承于ResourcePatternResolver默认情况下其getResources方法会委托给PathMatchingResourcePatternResolver执行
可以通过两种方式使用ResourcePatternResolver的能力
//使用PathMatchingResourcePatternResolver加载
ResourcePatternResolver resourcePatternResolvernew PathMatchingResourcePatternResolver();
Resource[] resourcesresourcePatternResolver.getResources(classpath*:META-INF/spring.handlers);//使用ApplicationContext加载
AnnotationConfigApplicationContext applicationContextnew AnnotationConfigApplicationContext();
applicationContext.refresh();
Resource[] resourcesapplicationContext.getResources(classpath*:META-INF/spring.handlers);
和ResourceLoader一样ResourcePatternResolver同样支持脱离Spring容器使用通过直接new PathMatchingResourcePatternResolver()的方式使用非常方便默认情况下ApplicationContext#getResources方法会委托给PathMatchingResourcePatternResolver执行因此只要可以拿到ApplicationContext实例也可以进行基于正则的批量资源加载