@@ -465,12 +465,12 @@ application components instead of `ResourceLoader`.
465
465
== Resources as Dependencies
466
466
467
467
If the bean itself is going to determine and supply the resource path through some sort
468
- of dynamic process, it probably makes sense for the bean to use the `ResourceLoader`
469
- interface to load resources. For example, consider the loading of a template of some
470
- sort, where the specific resource that is needed depends on the role of the user. If the
471
- resources are static, it makes sense to eliminate the use of the `ResourceLoader`
472
- interface completely, have the bean expose the `Resource` properties it needs,
473
- and expect them to be injected into it.
468
+ of dynamic process, it probably makes sense for the bean to use the `ResourceLoader` or
469
+ `ResourcePatternResolver` interface to load resources. For example, consider the loading
470
+ of a template of some sort, where the specific resource that is needed depends on the
471
+ role of the user. If the resources are static, it makes sense to eliminate the use of the
472
+ `ResourceLoader` interface (or `ResourcePatternResolver` interface) completely, have the
473
+ bean expose the `Resource` properties it needs, and expect them to be injected into it.
474
474
475
475
What makes it trivial to then inject these properties is that all application contexts
476
476
register and use a special JavaBeans `PropertyEditor`, which can convert `String` paths
@@ -484,14 +484,14 @@ be configured with a simple string for that resource, as the following example s
484
484
</bean>
485
485
----
486
486
487
- Note that the resource path has no prefix. Consequently, because the application context itself is
488
- going to be used as the `ResourceLoader`, the resource itself is loaded through a
489
- `ClassPathResource`, a `FileSystemResource`, or a `ServletContextResource`,
490
- depending on the exact type of the context.
487
+ Note that the resource path has no prefix. Consequently, because the application context
488
+ itself is going to be used as the `ResourceLoader`, the resource is loaded through a
489
+ `ClassPathResource`, a `FileSystemResource`, or a `ServletContextResource`, depending on
490
+ the exact type of the application context.
491
491
492
- If you need to force a specific `Resource` type to be used, you can use a prefix.
493
- The following two examples show how to force a `ClassPathResource` and a
494
- `UrlResource` (the latter being used to access a filesystem file):
492
+ If you need to force a specific `Resource` type to be used, you can use a prefix. The
493
+ following two examples show how to force a `ClassPathResource` and a `UrlResource` (the
494
+ latter being used to access a file in the filesystem ):
495
495
496
496
[source,xml,indent=0,subs="verbatim,quotes"]
497
497
----
@@ -552,12 +552,12 @@ used. However, consider the following example, which creates a `FileSystemXmlApp
552
552
val ctx = FileSystemXmlApplicationContext("conf/appContext.xml")
553
553
----
554
554
555
- Now the bean definition is loaded from a filesystem location (in this case, relative to
555
+ Now the bean definitions are loaded from a filesystem location (in this case, relative to
556
556
the current working directory).
557
557
558
558
Note that the use of the special `classpath` prefix or a standard URL prefix on the
559
- location path overrides the default type of `Resource` created to load the definition.
560
- Consider the following example:
559
+ location path overrides the default type of `Resource` created to load the bean
560
+ definitions. Consider the following example:
561
561
562
562
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
563
563
.Java
@@ -663,11 +663,11 @@ contents of the jar file to resolve the wildcards.
663
663
[[resources-app-ctx-portability]]
664
664
===== Implications on Portability
665
665
666
- If the specified path is already a file URL (either implicitly because the base
666
+ If the specified path is already a ` file` URL (either implicitly because the base
667
667
`ResourceLoader` is a filesystem one or explicitly), wildcarding is guaranteed to
668
668
work in a completely portable fashion.
669
669
670
- If the specified path is a classpath location, the resolver must obtain the last
670
+ If the specified path is a ` classpath` location, the resolver must obtain the last
671
671
non-wildcard path segment URL by making a `Classloader.getResource()` call. Since this
672
672
is just a node of the path (not the file at the end), it is actually undefined (in the
673
673
`ClassLoader` javadoc) exactly what sort of a URL is returned in this case. In practice,
@@ -754,7 +754,7 @@ avoiding the aforementioned portability problems with searching the jar file roo
754
754
====
755
755
756
756
Ant-style patterns with `classpath:` resources are not guaranteed to find matching
757
- resources if the root package to search is available in multiple class path locations.
757
+ resources if the root package to search is available in multiple classpath locations.
758
758
Consider the following example of a resource location:
759
759
760
760
[literal,subs="verbatim,quotes"]
@@ -769,12 +769,13 @@ Now consider an Ant-style path that someone might use to try to find that file:
769
769
classpath:com/mycompany/**/service-context.xml
770
770
----
771
771
772
- Such a resource may be in only one location, but when a path such as the preceding example
773
- is used to try to resolve it, the resolver works off the (first) URL returned by
774
- `getResource("com/mycompany");`. If this base package node exists in multiple
775
- `ClassLoader` locations, the actual end resource may not be there. Therefore, in such a case
776
- you should prefer using `classpath*:` with the same Ant-style pattern, which
777
- searches all class path locations that contain the root package.
772
+ Such a resource may exist in only one location in the classpath, but when a path such as
773
+ the preceding example is used to try to resolve it, the resolver works off the (first)
774
+ URL returned by `getResource("com/mycompany");`. If this base package node exists in
775
+ multiple `ClassLoader` locations, the desired resource may not exist in the first
776
+ location found. Therefore, in such cases you should prefer using `classpath*:` with the
777
+ same Ant-style pattern, which searches all classpath locations that contain the
778
+ `com.mycompany` base package: `classpath*:com/mycompany/**/service-context.xml`.
778
779
779
780
780
781
0 commit comments