@@ -260,7 +260,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
260
260
261
261
private PathMatcher pathMatcher = new AntPathMatcher ();
262
262
263
- private boolean useCaches = true ;
263
+ @ Nullable
264
+ private Boolean useCaches ;
264
265
265
266
private final Map <String , Resource []> rootDirCache = new ConcurrentHashMap <>();
266
267
@@ -342,10 +343,12 @@ public PathMatcher getPathMatcher() {
342
343
* the {@link JarURLConnection} level as well as within this resolver instance.
343
344
* <p>Note that {@link JarURLConnection#setDefaultUseCaches} can be turned off
344
345
* independently. This resolver-level setting is designed to only enforce
345
- * {@code JarURLConnection#setUseCaches(false)} if necessary but otherwise
346
- * leaves the JVM-level default in place.
346
+ * {@code JarURLConnection#setUseCaches(true/false)} if necessary but otherwise
347
+ * leaves the JVM-level default in place (if this setter has not been called).
348
+ * <p>As of 6.2.10, this setting propagates to {@link UrlResource#setUseCaches}.
347
349
* @since 6.1.19
348
350
* @see JarURLConnection#setUseCaches
351
+ * @see UrlResource#setUseCaches
349
352
* @see #clearCache()
350
353
*/
351
354
public void setUseCaches (boolean useCaches ) {
@@ -355,7 +358,11 @@ public void setUseCaches(boolean useCaches) {
355
358
356
359
@ Override
357
360
public Resource getResource (String location ) {
358
- return getResourceLoader ().getResource (location );
361
+ Resource resource = getResourceLoader ().getResource (location );
362
+ if (this .useCaches != null && resource instanceof UrlResource urlResource ) {
363
+ urlResource .setUseCaches (this .useCaches );
364
+ }
365
+ return resource ;
359
366
}
360
367
361
368
@ Override
@@ -473,20 +480,27 @@ protected Resource convertClassLoaderURL(URL url) {
473
480
}
474
481
}
475
482
else {
483
+ UrlResource resource = null ;
476
484
String urlString = url .toString ();
477
485
String cleanedPath = StringUtils .cleanPath (urlString );
478
486
if (!cleanedPath .equals (urlString )) {
479
487
// Prefer cleaned URL, aligned with UrlResource#createRelative(String)
480
488
try {
481
489
// Retain original URL instance, potentially including custom URLStreamHandler.
482
- return new UrlResource (new URL (url , cleanedPath ));
490
+ resource = new UrlResource (new URL (url , cleanedPath ));
483
491
}
484
492
catch (MalformedURLException ex ) {
485
493
// Fallback to regular URL construction below...
486
494
}
487
495
}
488
496
// Retain original URL instance, potentially including custom URLStreamHandler.
489
- return new UrlResource (url );
497
+ if (resource == null ) {
498
+ resource = new UrlResource (url );
499
+ }
500
+ if (this .useCaches != null ) {
501
+ resource .setUseCaches (this .useCaches );
502
+ }
503
+ return resource ;
490
504
}
491
505
}
492
506
@@ -505,6 +519,9 @@ protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set<
505
519
UrlResource jarResource = (ResourceUtils .URL_PROTOCOL_JAR .equals (url .getProtocol ()) ?
506
520
new UrlResource (url ) :
507
521
new UrlResource (ResourceUtils .JAR_URL_PREFIX + url + ResourceUtils .JAR_URL_SEPARATOR ));
522
+ if (this .useCaches != null ) {
523
+ jarResource .setUseCaches (this .useCaches );
524
+ }
508
525
if (jarResource .exists ()) {
509
526
result .add (jarResource );
510
527
}
@@ -556,7 +573,7 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
556
573
Set <ClassPathManifestEntry > entries = this .manifestEntriesCache ;
557
574
if (entries == null ) {
558
575
entries = getClassPathManifestEntries ();
559
- if (this .useCaches ) {
576
+ if (this .useCaches == null || this . useCaches ) {
560
577
this .manifestEntriesCache = entries ;
561
578
}
562
579
}
@@ -577,7 +594,7 @@ private Set<ClassPathManifestEntry> getClassPathManifestEntries() {
577
594
try {
578
595
File jar = new File (path ).getAbsoluteFile ();
579
596
if (jar .isFile () && seen .add (jar )) {
580
- manifestEntries .add (ClassPathManifestEntry .of (jar ));
597
+ manifestEntries .add (ClassPathManifestEntry .of (jar , this . useCaches ));
581
598
manifestEntries .addAll (getClassPathManifestEntriesFromJar (jar ));
582
599
}
583
600
}
@@ -616,7 +633,7 @@ private Set<ClassPathManifestEntry> getClassPathManifestEntriesFromJar(File jar)
616
633
}
617
634
File candidate = new File (parent , path );
618
635
if (candidate .isFile () && candidate .getCanonicalPath ().contains (parent .getCanonicalPath ())) {
619
- manifestEntries .add (ClassPathManifestEntry .of (candidate ));
636
+ manifestEntries .add (ClassPathManifestEntry .of (candidate , this . useCaches ));
620
637
}
621
638
}
622
639
}
@@ -710,7 +727,7 @@ else if (commonPrefix.equals(rootDirPath)) {
710
727
if (rootDirResources == null ) {
711
728
// Lookup for specific directory, creating a cache entry for it.
712
729
rootDirResources = getResources (rootDirPath );
713
- if (this .useCaches ) {
730
+ if (this .useCaches == null || this . useCaches ) {
714
731
this .rootDirCache .put (rootDirPath , rootDirResources );
715
732
}
716
733
}
@@ -729,7 +746,11 @@ else if (commonPrefix.equals(rootDirPath)) {
729
746
if (resolvedUrl != null ) {
730
747
rootDirUrl = resolvedUrl ;
731
748
}
732
- rootDirResource = new UrlResource (rootDirUrl );
749
+ UrlResource urlResource = new UrlResource (rootDirUrl );
750
+ if (this .useCaches != null ) {
751
+ urlResource .setUseCaches (this .useCaches );
752
+ }
753
+ rootDirResource = urlResource ;
733
754
}
734
755
if (rootDirUrl .getProtocol ().startsWith (ResourceUtils .URL_PROTOCOL_VFS )) {
735
756
result .addAll (VfsResourceMatchingDelegate .findMatchingResources (rootDirUrl , subPattern , getPathMatcher ()));
@@ -865,8 +886,8 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
865
886
866
887
if (con instanceof JarURLConnection jarCon ) {
867
888
// Should usually be the case for traditional JAR files.
868
- if (! this .useCaches ) {
869
- jarCon .setUseCaches (false );
889
+ if (this .useCaches != null ) {
890
+ jarCon .setUseCaches (this . useCaches );
870
891
}
871
892
try {
872
893
jarFile = jarCon .getJarFile ();
@@ -931,7 +952,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
931
952
}
932
953
}
933
954
}
934
- if (this .useCaches ) {
955
+ if (this .useCaches == null || this . useCaches ) {
935
956
// Cache jar entries in TreeSet for efficient searching on re-encounter.
936
957
this .jarEntriesCache .put (jarFileUrl , entriesCache );
937
958
}
@@ -1257,10 +1278,10 @@ private record ClassPathManifestEntry(Resource resource, @Nullable Resource alte
1257
1278
1258
1279
private static final String JARFILE_URL_PREFIX = ResourceUtils .JAR_URL_PREFIX + ResourceUtils .FILE_URL_PREFIX ;
1259
1280
1260
- static ClassPathManifestEntry of (File file ) throws MalformedURLException {
1281
+ static ClassPathManifestEntry of (File file , @ Nullable Boolean useCaches ) throws MalformedURLException {
1261
1282
String path = fixPath (file .getAbsolutePath ());
1262
- Resource resource = asJarFileResource (path );
1263
- Resource alternative = createAlternative (path );
1283
+ Resource resource = asJarFileResource (path , useCaches );
1284
+ Resource alternative = createAlternative (path , useCaches );
1264
1285
return new ClassPathManifestEntry (resource , alternative );
1265
1286
}
1266
1287
@@ -1281,18 +1302,22 @@ private static String fixPath(String path) {
1281
1302
* @return the alternative form or {@code null}
1282
1303
*/
1283
1304
@ Nullable
1284
- private static Resource createAlternative (String path ) {
1305
+ private static Resource createAlternative (String path , @ Nullable Boolean useCaches ) {
1285
1306
try {
1286
1307
String alternativePath = path .startsWith ("/" ) ? path .substring (1 ) : "/" + path ;
1287
- return asJarFileResource (alternativePath );
1308
+ return asJarFileResource (alternativePath , useCaches );
1288
1309
}
1289
1310
catch (MalformedURLException ex ) {
1290
1311
return null ;
1291
1312
}
1292
1313
}
1293
1314
1294
- private static Resource asJarFileResource (String path ) throws MalformedURLException {
1295
- return new UrlResource (JARFILE_URL_PREFIX + path + ResourceUtils .JAR_URL_SEPARATOR );
1315
+ private static Resource asJarFileResource (String path , @ Nullable Boolean useCaches ) throws MalformedURLException {
1316
+ UrlResource resource = new UrlResource (JARFILE_URL_PREFIX + path + ResourceUtils .JAR_URL_SEPARATOR );
1317
+ if (useCaches != null ) {
1318
+ resource .setUseCaches (useCaches );
1319
+ }
1320
+ return resource ;
1296
1321
}
1297
1322
}
1298
1323
0 commit comments