@@ -127,15 +127,15 @@ that context implementation or, via special prefixes on the `String` path, let t
127
127
caller specify that a specific `Resource` implementation must be created and used.
128
128
129
129
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.
132
132
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
134
134
considered equivalent to any other library you would use for this purpose.
135
135
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.
139
139
140
140
141
141
@@ -158,20 +158,19 @@ Spring includes the following `Resource` implementations:
158
158
=== `UrlResource`
159
159
160
160
`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.
166
166
167
167
A `UrlResource` is created by Java code by explicitly using the `UrlResource` constructor
168
168
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`.
175
174
176
175
177
176
@@ -182,7 +181,7 @@ This class represents a resource that should be obtained from the classpath. It
182
181
either the thread context class loader, a given class loader, or a given class for
183
182
loading resources.
184
183
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
186
185
resource resides in the file system but not for classpath resources that reside in a
187
186
jar and have not been expanded (by the servlet engine or whatever the environment is)
188
187
to the filesystem. To address this, the various `Resource` implementations always support
@@ -221,14 +220,15 @@ dependent on the Servlet container.
221
220
[[resources-implementations-inputstreamresource]]
222
221
=== `InputStreamResource`
223
222
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.
227
227
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.
232
232
233
233
234
234
@@ -256,15 +256,19 @@ interface definition:
256
256
public interface ResourceLoader {
257
257
258
258
Resource getResource(String location);
259
+
260
+ ClassLoader getClassLoader();
259
261
}
260
262
----
261
263
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
262
264
.Kotlin
263
265
----
264
- interface ResourceLoader {
266
+ interface ResourceLoader {
267
+
268
+ fun getResource(location: String): Resource
265
269
266
- fun getResource(location: String ): Resource
267
- }
270
+ fun getClassLoader( ): ClassLoader
271
+ }
268
272
----
269
273
270
274
All application contexts implement the `ResourceLoader` interface. Therefore, all
@@ -286,9 +290,9 @@ snippet of code was run against a `ClassPathXmlApplicationContext` instance:
286
290
val template = ctx.getResource("some/resource/path/myTemplate.txt")
287
291
----
288
292
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
292
296
`ServletContextResource`. It would similarly return appropriate objects for each context.
293
297
294
298
As a result, you can load resources in a fashion appropriate to the particular application
@@ -310,8 +314,7 @@ example shows:
310
314
----
311
315
312
316
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:
315
318
316
319
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
317
320
.Java
@@ -335,7 +338,8 @@ prefixes:
335
338
val template = ctx.getResource("https://myhost.com/resource/path/myTemplate.txt")
336
339
----
337
340
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:
339
343
340
344
[[resources-resource-strings]]
341
345
.Resource strings
@@ -350,7 +354,7 @@ The following table summarizes the strategy for converting `String` objects to `
350
354
| `file:///data/config.xml`
351
355
| Loaded as a `URL` from the filesystem. See also <<resources-filesystemresource-caveats>>.
352
356
353
- | http :
357
+ | https :
354
358
| `https://myserver/logo.png`
355
359
| Loaded as a `URL`.
356
360
@@ -366,8 +370,8 @@ The following table summarizes the strategy for converting `String` objects to `
366
370
== The `ResourceLoaderAware` interface
367
371
368
372
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:
371
375
372
376
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
373
377
.Java
@@ -400,7 +404,7 @@ interface (which can be considered a utility interface) and not to the whole Spr
400
404
`ApplicationContext` interface.
401
405
402
406
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_
404
408
`constructor` and `byType` autowiring modes (as described in <<beans-factory-autowire>>)
405
409
are capable of providing a `ResourceLoader` for either a constructor argument or a
406
410
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
507
511
Now the bean definition is loaded from a filesystem location (in this case, relative to
508
512
the current working directory).
509
513
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:
513
517
514
518
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
515
519
.Java
@@ -523,9 +527,9 @@ definition. Consider the following example:
523
527
val ctx = FileSystemXmlApplicationContext("classpath:conf/appContext.xml")
524
528
----
525
529
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.
529
533
530
534
531
535
[[resources-app-ctx-classpathxml]]
@@ -534,33 +538,34 @@ unprefixed paths are still treated as filesystem paths.
534
538
The `ClassPathXmlApplicationContext` exposes a number of constructors to enable
535
539
convenient instantiation. The basic idea is that you can supply merely a string array
536
540
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.
539
543
540
544
Consider the following directory layout:
541
545
542
546
[literal,subs="verbatim,quotes"]
543
547
----
544
548
com/
545
- foo /
549
+ example /
546
550
services.xml
547
- daos .xml
551
+ repositories .xml
548
552
MessengerService.class
549
553
----
550
554
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:
553
558
554
559
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
555
560
.Java
556
561
----
557
562
ApplicationContext ctx = new ClassPathXmlApplicationContext(
558
- new String[] {"services.xml", "daos .xml"}, MessengerService.class);
563
+ new String[] {"services.xml", "repositories .xml"}, MessengerService.class);
559
564
----
560
565
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
561
566
.Kotlin
562
567
----
563
- val ctx = ClassPathXmlApplicationContext(arrayOf("services.xml", "daos .xml"), MessengerService::class.java)
568
+ val ctx = ClassPathXmlApplicationContext(arrayOf("services.xml", "repositories .xml"), MessengerService::class.java)
564
569
----
565
570
566
571
See the {api-spring-framework}/context/support/ClassPathXmlApplicationContext.html[`ClassPathXmlApplicationContext`]
@@ -572,13 +577,13 @@ javadoc for details on the various constructors.
572
577
=== Wildcards in Application Context Constructor Resource Paths
573
578
574
579
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
577
582
(matched by using Spring's `PathMatcher` utility). Both of the latter are effectively
578
583
wildcards.
579
584
580
585
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,
582
587
when the final application context is created using the same path prefixed with
583
588
`classpath*:`, all component fragments are automatically picked up.
584
589
@@ -602,14 +607,14 @@ file:C:/some/path/\*-context.xml
602
607
classpath:com/mycompany/**/applicationContext.xml
603
608
----
604
609
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.
613
618
614
619
[[resources-app-ctx-portability]]
615
620
===== Implications on Portability
@@ -657,14 +662,15 @@ must be obtained (internally, this essentially happens through a call to
657
662
context definition.
658
663
659
664
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`
661
666
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
663
668
within a jar on the classpath:
664
669
`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.
668
674
669
675
You can also combine the `classpath*:` prefix with a `PathMatcher` pattern in the
670
676
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.
692
698
[NOTE]
693
699
====
694
700
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`
696
702
switch of the JAR task. Also, classpath directories may not get exposed based on security
697
703
policies in some environments -- for example, stand-alone applications on JDK 1.7.0_45
698
704
and higher (which requires 'Trusted-Library' to be set up in your manifests. See
@@ -722,7 +728,7 @@ classpath:com/mycompany/**/service-context.xml
722
728
Such a resource may be in only one location, but when a path such as the preceding example
723
729
is used to try to resolve it, the resolver works off the (first) URL returned by
724
730
`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
726
732
you should prefer using `classpath*:` with the same Ant-style pattern, which
727
733
searches all class path locations that contain the root package.
728
734
0 commit comments