PathMatchingResourcePatternResolver class
The ResourcePatternResolver used everywhere: it expands Ant-style
patterns (*, **, ?) in file: and classpath locations.
The application context uses one, so applications usually go through
the context; create one to search files without a context, in a tool
or a test. Patterns work for file: and classpath locations
(resources:, resources*: or no prefix, as in
DefaultResourceLoader); other locations go to resourceLoader as
they are, without wildcard expansion.
The classpath roots are the directories added with
ClasspathResources.addDirectory (latest first), then the embedded
resources together with lib/resources/. As in Spring, a resources:
pattern searches only the first root that has its base directory,
while resources*: searches every root, and resources*: without
wildcards gives the resource of every root that has it.
Matching files and directories are returned, sorted by path within a
root; directories the pattern cannot match below are not walked. The
pattern is cleaned first, as Spring cleans it: the directory before
the first wildcard is normalized as a classpath location is
(resources:./mail/../mail//*.txt is resources:mail/*.txt), and in
the rest, as by Spring's StringUtils.cleanPath, \\ (two
backslashes) is read as one /, then each \ left as /, and . and
.. segments are resolved (resources:mail\*.txt,
resources:mail/*/../*.txt and resources:mail/*\\..\\*.txt are
resources:mail/*.txt). As in Spring, a {name:regex} must not use
\, which becomes / (resources:mail/{n:\w+}.txt matches nothing):
write character classes such as [0-9] or [a-zA-Z0-9_] instead.
As in Spring, the wildcards of a file: pattern are those written:
only the directory before the first one is percent-decoded
(file:my%20dir/*.txt searches my dir/), so file:mail/%2A.txt is
the one file named *.txt, and the rest matches a % as it is.
Kept differences from Spring 7:
- a
..that climbs above the roots, or above the directory before the first wildcard, finds nothing (Spring'sresources*:mail/../../*.txtcan reach files next to a classpath directory; here nothing outside the roots is found); - a
\in the directory before the first wildcard of aresources*:pattern is cleaned too (resources*:conf\sub/*.txtfindsconf/sub/*.txt, asresources:conf\sub/*.txtdoes in both), where Spring hands the name to Java's class loader, which refuses it; - a
file:pattern whose separators are\there is read the same way, so Windows paths work (file:lib\resources\mail\*.txt,file:C:\config\*.properties), where Spring on macOS and Linux finds nothing (Invalid URI [file:]: its root directory ends at the last/); - a
resources:pattern whose base directory no root has (resources:nodir/*.txt) gives an empty list, where Spring throws aFileNotFoundException(cannot be resolved to URL because it does not exist).
// A tool that adds every properties file of ./config/ to its
// environment, then finds the message bundles of all packages.
final resolver = PathMatchingResourcePatternResolver();
for (final resource in resolver.getResources('file:config/*.properties')) {
environment.propertySources.addLast(
ResourcePropertySource.fromResource(resource),
);
}
resolver.getResources('resources*:i18n/messages*.properties');
Spring (Java):
var resolver = new PathMatchingResourcePatternResolver();
Resource[] files = resolver.getResources("file:config/*.properties");
for (Resource resource : files) {
environment.getPropertySources()
.addLast(new ResourcePropertySource(resource));
}
resolver.getResources("resources*:i18n/messages*.properties");
- Implemented types
Constructors
- PathMatchingResourcePatternResolver({ResourceLoader? resourceLoader, PathMatcher? pathMatcher})
-
Creates a resolver that loads single resources with
resourceLoader(a DefaultResourceLoader unless given) and matches paths withpathMatcher(an AntPathMatcher unless given).
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- pathMatcher → PathMatcher
-
The matcher that compares paths with the pattern.
final
- resourceLoader → ResourceLoader
-
The loader for locations without wildcards and for prefixes this
resolver does not expand.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
getResource(
String location) → Resource -
The resource at
location. Nothing is read yet, and the resource may not exist: check Resource.exists before reading when it may be missing. Never returnsnull; an unknown prefix gives a resource that does not exist.override -
getResources(
String location) → List< Resource> -
All resources matching
locationPattern, possibly none. A location without wildcards gives that one resource when it exists.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- classpathAllUrlPrefix → const String
-
The prefix that searches every classpath root instead of the first
one that has the base directory:
resources*:, Spring'sCLASSPATH_ALL_URL_PREFIX.