上一节讲完了资源加载器,在demo中可以看到单个资源加载的简单用例。本节讲一下ant格式路径的资源加载,
在spring中我们经常看到引入资源的时候会用到
@Value("classpath*:/META-INF/*.properties")
public Resource[] resources;
这种模式的路径,在spring中会解析这条路径,然后查找符合的资源,上面的path就是指向:classpath路径下/META-INF/文件下所有的properties文件
在图片中的三个文件中,符合规范的就是 application.properties和 config.properties两个文件
那么spring中是如何解析Ant格式的路径的呢,这个就要依赖一个接口 ResourcePatternResover
/**
* Strategy interface for resolving a location pattern (for example,
* an Ant-style path pattern) into Resource objects.
*
* <p>Can be used with any sort of location pattern (e.g. "/WEB-INF/*-context.xml"):
* Input patterns have to match the strategy implementation. This interface just
* specifies the conversion method rather than a specific pattern format.
*
* <p>This interface also suggests a new resource prefix "classpath*:" for all
* matching resources from the class path. Note that the resource location is
* expected to be a path without placeholders in this case (e.g. "/beans.xml");
* JAR files or classes directories can contain multiple files of the same name.
* @see org.springframework.core.io.Resource
* @see org.springframework.core.io.ResourceLoader
*/
public interface ResourcePatternResolver extends ResourceLoader {
/**
* Pseudo URL prefix for all matching resources from the class path: "classpath*:"
* This differs from ResourceLoader's classpath URL prefix in that it
* retrieves all matching resources for a given name (e.g. "/beans.xml"),
* for example in the root of all deployed JAR files.
* @see org.springframework.core.io.ResourceLoader#CLASSPATH_URL_PREFIX
*/
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
/**
* Resolve the given location pattern into Resource objects.
* <p>Overlapping resource entries that point to the same physical
* resource should be avoided, as far as possible. The result should
* have set semantics.
* @param locationPattern the location pattern to resolve
* @return the corresponding Resource ob