Skip to content

Commit 6b70544

Browse files
committed
Document ResourcePatternResolver in the reference manual
Closes gh-26447
1 parent c26087e commit 6b70544

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

src/docs/asciidoc/core/core-resources.adoc

+66-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Spring. It includes the following topics:
88
* <<resources-resource>>
99
* <<resources-implementations>>
1010
* <<resources-resourceloader>>
11+
* <<resources-resourcepatternresolver>>
1112
* <<resources-resourceloaderaware>>
1213
* <<resources-as-dependencies>>
1314
* <<resources-app-ctx>>
@@ -31,7 +32,7 @@ such as a method to check for the existence of the resource being pointed to.
3132

3233

3334
[[resources-resource]]
34-
== The Resource Interface
35+
== The `Resource` Interface
3536

3637
Spring's `Resource` interface located in the `org.springframework.core.io.` package is
3738
meant to be a more capable interface for abstracting access to low-level resources. The
@@ -176,7 +177,7 @@ work.
176177

177178

178179
[[resources-implementations]]
179-
== Built-in Resource Implementations
180+
== Built-in `Resource` Implementations
180181

181182
Spring includes several built-in `Resource` implementations, including but not limited to
182183
the following:
@@ -242,6 +243,7 @@ transformations but performing all operations via the `java.nio.file.Files` API.
242243
supports resolution as a `File` and as a `URL`.
243244

244245

246+
245247
[[resources-implementations-pathresource]]
246248
=== `PathResource`
247249

@@ -293,8 +295,9 @@ single-use `InputStreamResource`.
293295

294296

295297

298+
296299
[[resources-resourceloader]]
297-
== The `ResourceLoader`
300+
== The `ResourceLoader` Interface
298301

299302
The `ResourceLoader` interface is meant to be implemented by objects that can return
300303
(that is, load) `Resource` instances. The following listing shows the `ResourceLoader`
@@ -416,8 +419,66 @@ objects:
416419

417420

418421

422+
[[resources-resourcepatternresolver]]
423+
== The `ResourcePatternResolver` Interface
424+
425+
The `ResourcePatternResolver` interface is an extension to the `ResourceLoader` interface
426+
which defines a strategy for resolving a location pattern (for example, an Ant-style path
427+
pattern) into `Resource` objects.
428+
429+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
430+
.Java
431+
----
432+
public interface ResourcePatternResolver extends ResourceLoader {
433+
434+
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
435+
436+
Resource[] getResources(String locationPattern) throws IOException;
437+
438+
}
439+
----
440+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
441+
.Kotlin
442+
----
443+
interface ResourcePatternResolver: ResourceLoader {
444+
445+
@JvmField
446+
val CLASSPATH_ALL_URL_PREFIX: String = "classpath*:"
447+
448+
@Throws(IOException::class)
449+
fun getResources(locationPattern: String): Resource[]
450+
451+
}
452+
----
453+
454+
As can be seen above, this interface also defines a special `classpath*:` resource prefix
455+
for all matching resources from the class path. Note that the resource location is
456+
expected to be a path without placeholders in this case -- for example,
457+
`classpath*:/config/beans.xml`. JAR files or different directories in the class path can
458+
contain multiple files with the same path and the same name. See
459+
<<resources-app-ctx-wildcards-in-resource-paths>> and its subsections for further details
460+
on wildcard support with the `classpath*:` resource prefix.
461+
462+
A passed-in `ResourceLoader` (for example, one supplied via
463+
<<resources-resourceloaderaware, `ResourceLoaderAware`>> semantics can be checked whether
464+
it implements this extended interface too.
465+
466+
`PathMatchingResourcePatternResolver` is a standalone implementation that is usable
467+
outside an `ApplicationContext` and is also used by `ResourceArrayPropertyEditor` for
468+
populating `Resource[]` bean properties.
469+
470+
[NOTE]
471+
====
472+
The default `ResourceLoader` in any standard `ApplicationContext` is in fact an instance
473+
of `PathMatchingResourcePatternResolver` which implements the `ResourcePatternResolver`
474+
interface. The same is true for the `ApplicationContext` instance itself which also
475+
implements the `ResourcePatternResolver` interface and delegates to the default
476+
`PathMatchingResourcePatternResolver`.
477+
====
478+
479+
419480
[[resources-resourceloaderaware]]
420-
== The `ResourceLoaderAware` interface
481+
== The `ResourceLoaderAware` Interface
421482

422483
The `ResourceLoaderAware` interface is a special callback interface which identifies
423484
components that expect to be provided a `ResourceLoader` reference. The following listing
@@ -628,7 +689,7 @@ javadoc for details on the various constructors.
628689

629690
The resource paths in application context constructor values may be simple paths (as
630691
shown earlier), each of which has a one-to-one mapping to a target `Resource` or,
631-
alternately, may contain the special "classpath*:" prefix or internal Ant-style patterns
692+
alternately, may contain the special `classpath*:` prefix or internal Ant-style patterns
632693
(matched by using Spring's `PathMatcher` utility). Both of the latter are effectively
633694
wildcards.
634695

0 commit comments

Comments
 (0)