Resouce介绍
资源管理在工作中我们平时并不怎么用,一般都是写好的,我们也没有深究过其中的原理,今天我就看着spring官方文档结合源码看看这一块的知识进行一个梳理.
Resource接口在org.springframework.core.io
public interface Resource extends InputStreamSource {
boolean exists();
default boolean isReadable() {
return this.exists();
}
default boolean isOpen() {
return false;
}
default boolean isFile() {
return false;
}
URL getURL() throws IOException;
URI getURI() throws IOException;
File getFile() throws IOException;
default ReadableByteChannel readableChannel() throws IOException {
return Channels.newChannel(this.getInputStream());
}
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String var1) throws IOException;
@Nullable
String getFilename();
String getDescription();
}
spring 自己实现的几个常用
ResourceLoader Interface
这个接口就是对象可以获取 Resource 和 ClassLoader
public interface ResourceLoader {
Resource getResource(String location);
ClassLoader getClassLoader();
}
All application contexts implement the ResourceLoader interface. Therefore, all application contexts may be used to obtain Resource instances
所有的应用程序上下文都实现了ResourceLoader接口。因此,所有应用程序上下文都可以用来获取资源实例
Resource resource = context.getResource("demo.txt");
String filename = resource.getFilename();
URL resource4 = context.getClassLoader().getResource("demo.txt");
System.out.println(resource4.getFile());
System.out.println(filename);
Resource resource1 = context.getResource("classpath:wfg/aa.txt");
String filename1 = resource1.getFilename();
System.out.println(filename1);
Resource resource2 = context.getResource("file:wfg/aa.txt");
String filename2 = resource2.getFilename();
System.out.println(filename2);
Resource resource3 = context.getResource("https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#resources-introduction");
String filename3 = resource3.getFilename();
System.out.println(filename3);
ResourcePatternResolver
The ResourcePatternResolver interface is an extension to the ResourceLoader interface which defines a strategy for resolving a location pattern (for example, an Ant-style path pattern) into Resource objects.
ResourcePatternResolver接口是ResourceLoader接口的扩展,该接口定义了将位置模式(例如,ant风格的路径模式)解析为资源对象的策略
简单理解就是支持通配符
public interface ResourcePatternResolver extends ResourceLoader {
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
Resource[] getResources(String var1) throws IOException;
}
“classpath*:” 可以获取所有的包括jar中的文件
JAR files or different directories in the class path can contain multiple files with the same path and the same name
PathMatchingResourcePatternResolver 是一个独立的实现,完全可以在 ApplicationContext 之外运行,
PathMatchingResourcePatternResolver 可以将制定的资源路径解析成一个或多个Resource 对象,
资源路径可以是普通的,一对一的,也可以是使用通配符
PathMatchingResourcePatternResolver is a standalone implementation that is usable outside an ApplicationContext and is also used by ResourceArrayPropertyEditor for populating Resource[] bean properties. PathMatchingResourcePatternResolver is able to resolve a specified resource location path into one or more matching Resource objects. The source path may be a simple path which has a one-to-one mapping to a target Resource, or alternatively may contain the special classpath*: prefix and/or internal Ant-style regular expressions (matched using Spring’s org.springframework.util.AntPathMatcher utility). Both of the latter are effectively wildcards
注意:在任何标准的应用上下文中,ResourceLoader 是实现ResourcePatternResolver 这个接口的一个PathMatchingResourcePatternResolver 的实例,ApplicationContext 自身也是实现ResourcePatternResolver 接口,并委派给默认的PathMatchingResourcePatternResolver
ResourceLoaderAware Interface
ResourceLoaderAware 实现这个接口可以自定义ResourceLoader
完整demo代码
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
IUserService bean = context.getBean(IUserService.class);
System.out.println(bean);
Resource resource = context.getResource("demo.txt");
String filename = resource.getFilename();
URL resource4 = context.getClassLoader().getResource("demo.txt");
System.out.println(resource4.getFile());
System.out.println(filename);
Resource resource1 = context.getResource("classpath:wfg/aa.txt");
String filename1 = resource1.getFilename();
System.out.println(filename1);
Resource resource2 = context.getResource("file:wfg/aa.txt");
String filename2 = resource2.getFilename();
System.out.println(filename2);
Resource resource3 = context.getResource("https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#resources-introduction");
String filename3 = resource3.getFilename();
System.out.println(filename3);
Resource[] resources = new Resource[0];
try {
resources = context.getResources("classpath:*");
for (int i = 0; i < resources.length; i++) {
System.out.println(resources[i].getURL());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这个知识点在工作中不是很重要,了解就可以