Skip to content

Commit ff43731

Browse files
committed
Polish Resources section of the reference manual
See gh-26447
1 parent 29fb3f2 commit ff43731

File tree

1 file changed

+76
-70
lines changed

1 file changed

+76
-70
lines changed

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

+76-70
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ that context implementation or, via special prefixes on the `String` path, let t
127127
caller specify that a specific `Resource` implementation must be created and used.
128128

129129
While the `Resource` interface is used a lot with Spring and by Spring, it is actually
130-
very useful to use as a general utility class by itself in your own code, for access to
131-
resources, even when your code does not know or care about any other parts of Spring.
130+
very convenient to use as a general utility class by itself in your own code, for access
131+
to resources, even when your code does not know or care about any other parts of Spring.
132132
While this couples your code to Spring, it really only couples it to this small set of
133-
utility classes, which serve as a more capable replacement for `URL` and can be
133+
utility classes, which serves as a more capable replacement for `URL` and can be
134134
considered equivalent to any other library you would use for this purpose.
135135

136-
NOTE: The `Resource` abstraction does not replace functionality.
137-
It wraps it where possible. For example, a `UrlResource` wraps a URL and uses the
138-
wrapped `URL` to do its work.
136+
NOTE: The `Resource` abstraction does not replace functionality. It wraps it where
137+
possible. For example, a `UrlResource` wraps a URL and uses the wrapped `URL` to do its
138+
work.
139139

140140

141141

@@ -158,20 +158,19 @@ Spring includes the following `Resource` implementations:
158158
=== `UrlResource`
159159

160160
`UrlResource` wraps a `java.net.URL` and can be used to access any object that is
161-
normally accessible with a URL, such as files, an HTTP target, an FTP target, and others. All
162-
URLs have a standardized `String` representation, such that appropriate standardized
163-
prefixes are used to indicate one URL type from another. This includes `file:` for
164-
accessing filesystem paths, `http:` for accessing resources through the HTTP protocol,
165-
`ftp:` for accessing resources through FTP, and others.
161+
normally accessible with a URL, such as files, an HTTPS target, an FTP target, and
162+
others. All URLs have a standardized `String` representation, such that appropriate
163+
standardized prefixes are used to indicate one URL type from another. This includes
164+
`file:` for accessing filesystem paths, `https:` for accessing resources through the
165+
HTTPS protocol, `ftp:` for accessing resources through FTP, and others.
166166

167167
A `UrlResource` is created by Java code by explicitly using the `UrlResource` constructor
168168
but is often created implicitly when you call an API method that takes a `String`
169-
argument meant to represent a path. For the latter case, a JavaBeans
170-
`PropertyEditor` ultimately decides which type of `Resource` to create. If the path
171-
string contains well-known (to it, that is) prefix (such as `classpath:`), it
172-
creates an appropriate specialized `Resource` for that prefix. However, if it does not
173-
recognize the prefix, it assume the string is a standard URL string and
174-
creates a `UrlResource`.
169+
argument meant to represent a path. For the latter case, a JavaBeans `PropertyEditor`
170+
ultimately decides which type of `Resource` to create. If the path string contains a
171+
well-known (to property editor, that is) prefix (such as `classpath:`), it creates an
172+
appropriate specialized `Resource` for that prefix. However, if it does not recognize the
173+
prefix, it assumes the string is a standard URL string and creates a `UrlResource`.
175174

176175

177176

@@ -182,7 +181,7 @@ This class represents a resource that should be obtained from the classpath. It
182181
either the thread context class loader, a given class loader, or a given class for
183182
loading resources.
184183

185-
This `Resource` implementation supports resolution as `java.io.File` if the class path
184+
This `Resource` implementation supports resolution as a `java.io.File` if the class path
186185
resource resides in the file system but not for classpath resources that reside in a
187186
jar and have not been expanded (by the servlet engine or whatever the environment is)
188187
to the filesystem. To address this, the various `Resource` implementations always support
@@ -221,14 +220,15 @@ dependent on the Servlet container.
221220
[[resources-implementations-inputstreamresource]]
222221
=== `InputStreamResource`
223222

224-
An `InputStreamResource` is a `Resource` implementation for a given `InputStream`. It should be used only if no
225-
specific `Resource` implementation is applicable. In particular, prefer
226-
`ByteArrayResource` or any of the file-based `Resource` implementations where possible.
223+
An `InputStreamResource` is a `Resource` implementation for a given `InputStream`. It
224+
should be used only if no specific `Resource` implementation is applicable. In
225+
particular, prefer `ByteArrayResource` or any of the file-based `Resource`
226+
implementations where possible.
227227

228-
In contrast to other `Resource` implementations, this is a descriptor for an already-opened
229-
resource. Therefore, it returns `true` from `isOpen()`. Do not use it if you need
230-
to keep the resource descriptor somewhere or if you need to read a stream multiple
231-
times.
228+
In contrast to other `Resource` implementations, this is a descriptor for an
229+
already-opened resource. Therefore, it returns `true` from `isOpen()`. Do not use it if
230+
you need to keep the resource descriptor somewhere or if you need to read a stream
231+
multiple times.
232232

233233

234234

@@ -256,15 +256,19 @@ interface definition:
256256
public interface ResourceLoader {
257257
258258
Resource getResource(String location);
259+
260+
ClassLoader getClassLoader();
259261
}
260262
----
261263
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
262264
.Kotlin
263265
----
264-
interface ResourceLoader {
266+
interface ResourceLoader {
267+
268+
fun getResource(location: String): Resource
265269
266-
fun getResource(location: String): Resource
267-
}
270+
fun getClassLoader(): ClassLoader
271+
}
268272
----
269273

270274
All application contexts implement the `ResourceLoader` interface. Therefore, all
@@ -286,9 +290,9 @@ snippet of code was run against a `ClassPathXmlApplicationContext` instance:
286290
val template = ctx.getResource("some/resource/path/myTemplate.txt")
287291
----
288292

289-
Against a `ClassPathXmlApplicationContext`, that code returns a `ClassPathResource`. If the same method were run
290-
against a `FileSystemXmlApplicationContext` instance, it would return a
291-
`FileSystemResource`. For a `WebApplicationContext`, it would return a
293+
Against a `ClassPathXmlApplicationContext`, that code returns a `ClassPathResource`. If
294+
the same method were run against a `FileSystemXmlApplicationContext` instance, it would
295+
return a `FileSystemResource`. For a `WebApplicationContext`, it would return a
292296
`ServletContextResource`. It would similarly return appropriate objects for each context.
293297

294298
As a result, you can load resources in a fashion appropriate to the particular application
@@ -310,8 +314,7 @@ example shows:
310314
----
311315

312316
Similarly, you can force a `UrlResource` to be used by specifying any of the standard
313-
`java.net.URL` prefixes. The following pair of examples use the `file` and `http`
314-
prefixes:
317+
`java.net.URL` prefixes. The following examples use the `file` and `https` prefixes:
315318

316319
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
317320
.Java
@@ -335,7 +338,8 @@ prefixes:
335338
val template = ctx.getResource("https://myhost.com/resource/path/myTemplate.txt")
336339
----
337340

338-
The following table summarizes the strategy for converting `String` objects to `Resource` objects:
341+
The following table summarizes the strategy for converting `String` objects to `Resource`
342+
objects:
339343

340344
[[resources-resource-strings]]
341345
.Resource strings
@@ -350,7 +354,7 @@ The following table summarizes the strategy for converting `String` objects to `
350354
| `file:///data/config.xml`
351355
| Loaded as a `URL` from the filesystem. See also <<resources-filesystemresource-caveats>>.
352356

353-
| http:
357+
| https:
354358
| `https://myserver/logo.png`
355359
| Loaded as a `URL`.
356360

@@ -366,8 +370,8 @@ The following table summarizes the strategy for converting `String` objects to `
366370
== The `ResourceLoaderAware` interface
367371

368372
The `ResourceLoaderAware` interface is a special callback interface which identifies
369-
components that expect to be provided with a `ResourceLoader` reference. The following
370-
listing shows the definition of the `ResourceLoaderAware` interface:
373+
components that expect to be provided a `ResourceLoader` reference. The following listing
374+
shows the definition of the `ResourceLoaderAware` interface:
371375

372376
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
373377
.Java
@@ -400,7 +404,7 @@ interface (which can be considered a utility interface) and not to the whole Spr
400404
`ApplicationContext` interface.
401405

402406
In application components, you may also rely upon autowiring of the `ResourceLoader` as
403-
an alternative to implementing the `ResourceLoaderAware` interface. The "`traditional`"
407+
an alternative to implementing the `ResourceLoaderAware` interface. The _traditional_
404408
`constructor` and `byType` autowiring modes (as described in <<beans-factory-autowire>>)
405409
are capable of providing a `ResourceLoader` for either a constructor argument or a
406410
setter method parameter, respectively. For more flexibility (including the ability to
@@ -507,9 +511,9 @@ used. However, consider the following example, which creates a `FileSystemXmlApp
507511
Now the bean definition is loaded from a filesystem location (in this case, relative to
508512
the current working directory).
509513

510-
Note that the use of the special classpath prefix or a standard URL prefix on the
511-
location path overrides the default type of `Resource` created to load the
512-
definition. Consider the following example:
514+
Note that the use of the special `classpath` prefix or a standard URL prefix on the
515+
location path overrides the default type of `Resource` created to load the definition.
516+
Consider the following example:
513517

514518
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
515519
.Java
@@ -523,9 +527,9 @@ definition. Consider the following example:
523527
val ctx = FileSystemXmlApplicationContext("classpath:conf/appContext.xml")
524528
----
525529

526-
Using `FileSystemXmlApplicationContext` loads the bean definitions from the classpath. However, it is still a
527-
`FileSystemXmlApplicationContext`. If it is subsequently used as a `ResourceLoader`, any
528-
unprefixed paths are still treated as filesystem paths.
530+
Using `FileSystemXmlApplicationContext` loads the bean definitions from the classpath.
531+
However, it is still a `FileSystemXmlApplicationContext`. If it is subsequently used as a
532+
`ResourceLoader`, any unprefixed paths are still treated as filesystem paths.
529533

530534

531535
[[resources-app-ctx-classpathxml]]
@@ -534,33 +538,34 @@ unprefixed paths are still treated as filesystem paths.
534538
The `ClassPathXmlApplicationContext` exposes a number of constructors to enable
535539
convenient instantiation. The basic idea is that you can supply merely a string array
536540
that contains only the filenames of the XML files themselves (without the leading path
537-
information) and also supplies a `Class`. The `ClassPathXmlApplicationContext`
538-
then derives the path information from the supplied class.
541+
information) and also supply a `Class`. The `ClassPathXmlApplicationContext` then derives
542+
the path information from the supplied class.
539543

540544
Consider the following directory layout:
541545

542546
[literal,subs="verbatim,quotes"]
543547
----
544548
com/
545-
foo/
549+
example/
546550
services.xml
547-
daos.xml
551+
repositories.xml
548552
MessengerService.class
549553
----
550554

551-
The following example shows how a `ClassPathXmlApplicationContext` instance composed of the beans defined in
552-
files named `services.xml` and `daos.xml` (which are on the classpath) can be instantiated:
555+
The following example shows how a `ClassPathXmlApplicationContext` instance composed of
556+
the beans defined in files named `services.xml` and `repositories.xml` (which are on the
557+
classpath) can be instantiated:
553558

554559
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
555560
.Java
556561
----
557562
ApplicationContext ctx = new ClassPathXmlApplicationContext(
558-
new String[] {"services.xml", "daos.xml"}, MessengerService.class);
563+
new String[] {"services.xml", "repositories.xml"}, MessengerService.class);
559564
----
560565
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
561566
.Kotlin
562567
----
563-
val ctx = ClassPathXmlApplicationContext(arrayOf("services.xml", "daos.xml"), MessengerService::class.java)
568+
val ctx = ClassPathXmlApplicationContext(arrayOf("services.xml", "repositories.xml"), MessengerService::class.java)
564569
----
565570

566571
See the {api-spring-framework}/context/support/ClassPathXmlApplicationContext.html[`ClassPathXmlApplicationContext`]
@@ -572,13 +577,13 @@ javadoc for details on the various constructors.
572577
=== Wildcards in Application Context Constructor Resource Paths
573578

574579
The resource paths in application context constructor values may be simple paths (as
575-
shown earlier), each of which has a one-to-one mapping to a target `Resource` or, alternately, may
576-
contain the special "classpath*:" prefix or internal Ant-style regular expressions
580+
shown earlier), each of which has a one-to-one mapping to a target `Resource` or,
581+
alternately, may contain the special "classpath*:" prefix or internal Ant-style patterns
577582
(matched by using Spring's `PathMatcher` utility). Both of the latter are effectively
578583
wildcards.
579584

580585
One use for this mechanism is when you need to do component-style application assembly. All
581-
components can 'publish' context definition fragments to a well-known location path, and,
586+
components can _publish_ context definition fragments to a well-known location path, and,
582587
when the final application context is created using the same path prefixed with
583588
`classpath*:`, all component fragments are automatically picked up.
584589

@@ -602,14 +607,14 @@ file:C:/some/path/\*-context.xml
602607
classpath:com/mycompany/**/applicationContext.xml
603608
----
604609

605-
When the path location contains an Ant-style pattern, the resolver follows a more complex procedure to try to resolve the
606-
wildcard. It produces a `Resource` for the path up to the last non-wildcard segment and
607-
obtains a URL from it. If this URL is not a `jar:` URL or container-specific variant
608-
(such as `zip:` in WebLogic, `wsjar` in WebSphere, and so on), a `java.io.File` is
609-
obtained from it and used to resolve the wildcard by traversing the filesystem. In the
610-
case of a jar URL, the resolver either gets a `java.net.JarURLConnection` from it or
611-
manually parses the jar URL and then traverses the contents of the jar file to resolve
612-
the wildcards.
610+
When the path location contains an Ant-style pattern, the resolver follows a more complex
611+
procedure to try to resolve the wildcard. It produces a `Resource` for the path up to the
612+
last non-wildcard segment and obtains a URL from it. If this URL is not a `jar:` URL or
613+
container-specific variant (such as `zip:` in WebLogic, `wsjar` in WebSphere, and so on),
614+
a `java.io.File` is obtained from it and used to resolve the wildcard by traversing the
615+
filesystem. In the case of a jar URL, the resolver either gets a
616+
`java.net.JarURLConnection` from it or manually parses the jar URL and then traverses the
617+
contents of the jar file to resolve the wildcards.
613618

614619
[[resources-app-ctx-portability]]
615620
===== Implications on Portability
@@ -657,14 +662,15 @@ must be obtained (internally, this essentially happens through a call to
657662
context definition.
658663

659664
NOTE: The wildcard classpath relies on the `getResources()` method of the underlying
660-
classloader. As most application servers nowadays supply their own classloader
665+
`ClassLoader`. As most application servers nowadays supply their own `ClassLoader`
661666
implementation, the behavior might differ, especially when dealing with jar files. A
662-
simple test to check if `classpath*` works is to use the classloader to load a file from
667+
simple test to check if `classpath*` works is to use the `ClassLoader` to load a file from
663668
within a jar on the classpath:
664669
`getClass().getClassLoader().getResources("<someFileInsideTheJar>")`. Try this test with
665-
files that have the same name but are placed inside two different locations. In case an
666-
inappropriate result is returned, check the application server documentation for
667-
settings that might affect the classloader behavior.
670+
files that have the same name but reside in two different locations -- for example, files
671+
with the same name and same path but in different jars on the classpath. In case an
672+
inappropriate result is returned, check the application server documentation for settings
673+
that might affect the `ClassLoader` behavior.
668674

669675
You can also combine the `classpath*:` prefix with a `PathMatcher` pattern in the
670676
rest of the location path (for example, `classpath*:META-INF/*-beans.xml`). In this
@@ -692,7 +698,7 @@ as well, but this is not guaranteed to lead to portable behavior.
692698
[NOTE]
693699
====
694700
The scanning of classpath packages requires the presence of corresponding directory
695-
entries in the classpath. When you build JARs with Ant, do not activate the files-only
701+
entries in the classpath. When you build JARs with Ant, do not activate the `files-only`
696702
switch of the JAR task. Also, classpath directories may not get exposed based on security
697703
policies in some environments -- for example, stand-alone applications on JDK 1.7.0_45
698704
and higher (which requires 'Trusted-Library' to be set up in your manifests. See
@@ -722,7 +728,7 @@ classpath:com/mycompany/**/service-context.xml
722728
Such a resource may be in only one location, but when a path such as the preceding example
723729
is used to try to resolve it, the resolver works off the (first) URL returned by
724730
`getResource("com/mycompany");`. If this base package node exists in multiple
725-
classloader locations, the actual end resource may not be there. Therefore, in such a case
731+
`ClassLoader` locations, the actual end resource may not be there. Therefore, in such a case
726732
you should prefer using `classpath*:` with the same Ant-style pattern, which
727733
searches all class path locations that contain the root package.
728734

0 commit comments

Comments
 (0)