@@ -259,7 +259,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
259
259
260
260
private PathMatcher pathMatcher = new AntPathMatcher ();
261
261
262
- private boolean useCaches = true ;
262
+ @ Nullable
263
+ private Boolean useCaches ;
263
264
264
265
private final Map <String , Resource []> rootDirCache = new ConcurrentHashMap <>();
265
266
@@ -339,10 +340,12 @@ public PathMatcher getPathMatcher() {
339
340
* the {@link JarURLConnection} level as well as within this resolver instance.
340
341
* <p>Note that {@link JarURLConnection#setDefaultUseCaches} can be turned off
341
342
* independently. This resolver-level setting is designed to only enforce
342
- * {@code JarURLConnection#setUseCaches(false)} if necessary but otherwise
343
- * leaves the JVM-level default in place.
343
+ * {@code JarURLConnection#setUseCaches(true/false)} if necessary but otherwise
344
+ * leaves the JVM-level default in place (if this setter has not been called).
345
+ * <p>As of 6.2.10, this setting propagates to {@link UrlResource#setUseCaches}.
344
346
* @since 6.1.19
345
347
* @see JarURLConnection#setUseCaches
348
+ * @see UrlResource#setUseCaches
346
349
* @see #clearCache()
347
350
*/
348
351
public void setUseCaches (boolean useCaches ) {
@@ -352,7 +355,11 @@ public void setUseCaches(boolean useCaches) {
352
355
353
356
@ Override
354
357
public Resource getResource (String location ) {
355
- return getResourceLoader ().getResource (location );
358
+ Resource resource = getResourceLoader ().getResource (location );
359
+ if (this .useCaches != null && resource instanceof UrlResource urlResource ) {
360
+ urlResource .setUseCaches (this .useCaches );
361
+ }
362
+ return resource ;
356
363
}
357
364
358
365
@ Override
@@ -470,20 +477,27 @@ protected Resource convertClassLoaderURL(URL url) {
470
477
}
471
478
}
472
479
else {
480
+ UrlResource resource = null ;
473
481
String urlString = url .toString ();
474
482
String cleanedPath = StringUtils .cleanPath (urlString );
475
483
if (!cleanedPath .equals (urlString )) {
476
484
// Prefer cleaned URL, aligned with UrlResource#createRelative(String)
477
485
try {
478
486
// Retain original URL instance, potentially including custom URLStreamHandler.
479
- return new UrlResource (new URL (url , cleanedPath ));
487
+ resource = new UrlResource (new URL (url , cleanedPath ));
480
488
}
481
489
catch (MalformedURLException ex ) {
482
490
// Fallback to regular URL construction below...
483
491
}
484
492
}
485
493
// Retain original URL instance, potentially including custom URLStreamHandler.
486
- return new UrlResource (url );
494
+ if (resource == null ) {
495
+ resource = new UrlResource (url );
496
+ }
497
+ if (this .useCaches != null ) {
498
+ resource .setUseCaches (this .useCaches );
499
+ }
500
+ return resource ;
487
501
}
488
502
}
489
503
@@ -502,6 +516,9 @@ protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set<
502
516
UrlResource jarResource = (ResourceUtils .URL_PROTOCOL_JAR .equals (url .getProtocol ()) ?
503
517
new UrlResource (url ) :
504
518
new UrlResource (ResourceUtils .JAR_URL_PREFIX + url + ResourceUtils .JAR_URL_SEPARATOR ));
519
+ if (this .useCaches != null ) {
520
+ jarResource .setUseCaches (this .useCaches );
521
+ }
505
522
if (jarResource .exists ()) {
506
523
result .add (jarResource );
507
524
}
@@ -553,7 +570,7 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
553
570
Set <ClassPathManifestEntry > entries = this .manifestEntriesCache ;
554
571
if (entries == null ) {
555
572
entries = getClassPathManifestEntries ();
556
- if (this .useCaches ) {
573
+ if (this .useCaches == null || this . useCaches ) {
557
574
this .manifestEntriesCache = entries ;
558
575
}
559
576
}
@@ -574,7 +591,7 @@ private Set<ClassPathManifestEntry> getClassPathManifestEntries() {
574
591
try {
575
592
File jar = new File (path ).getAbsoluteFile ();
576
593
if (jar .isFile () && seen .add (jar )) {
577
- manifestEntries .add (ClassPathManifestEntry .of (jar ));
594
+ manifestEntries .add (ClassPathManifestEntry .of (jar , this . useCaches ));
578
595
manifestEntries .addAll (getClassPathManifestEntriesFromJar (jar ));
579
596
}
580
597
}
@@ -613,7 +630,7 @@ private Set<ClassPathManifestEntry> getClassPathManifestEntriesFromJar(File jar)
613
630
}
614
631
File candidate = new File (parent , path );
615
632
if (candidate .isFile () && candidate .getCanonicalPath ().contains (parent .getCanonicalPath ())) {
616
- manifestEntries .add (ClassPathManifestEntry .of (candidate ));
633
+ manifestEntries .add (ClassPathManifestEntry .of (candidate , this . useCaches ));
617
634
}
618
635
}
619
636
}
@@ -707,7 +724,7 @@ else if (commonPrefix.equals(rootDirPath)) {
707
724
if (rootDirResources == null ) {
708
725
// Lookup for specific directory, creating a cache entry for it.
709
726
rootDirResources = getResources (rootDirPath );
710
- if (this .useCaches ) {
727
+ if (this .useCaches == null || this . useCaches ) {
711
728
this .rootDirCache .put (rootDirPath , rootDirResources );
712
729
}
713
730
}
@@ -726,7 +743,11 @@ else if (commonPrefix.equals(rootDirPath)) {
726
743
if (resolvedUrl != null ) {
727
744
rootDirUrl = resolvedUrl ;
728
745
}
729
- rootDirResource = new UrlResource (rootDirUrl );
746
+ UrlResource urlResource = new UrlResource (rootDirUrl );
747
+ if (this .useCaches != null ) {
748
+ urlResource .setUseCaches (this .useCaches );
749
+ }
750
+ rootDirResource = urlResource ;
730
751
}
731
752
if (rootDirUrl .getProtocol ().startsWith (ResourceUtils .URL_PROTOCOL_VFS )) {
732
753
result .addAll (VfsResourceMatchingDelegate .findMatchingResources (rootDirUrl , subPattern , getPathMatcher ()));
@@ -862,8 +883,8 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
862
883
863
884
if (con instanceof JarURLConnection jarCon ) {
864
885
// Should usually be the case for traditional JAR files.
865
- if (! this .useCaches ) {
866
- jarCon .setUseCaches (false );
886
+ if (this .useCaches != null ) {
887
+ jarCon .setUseCaches (this . useCaches );
867
888
}
868
889
try {
869
890
jarFile = jarCon .getJarFile ();
@@ -928,7 +949,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
928
949
}
929
950
}
930
951
}
931
- if (this .useCaches ) {
952
+ if (this .useCaches == null || this . useCaches ) {
932
953
// Cache jar entries in TreeSet for efficient searching on re-encounter.
933
954
this .jarEntriesCache .put (jarFileUrl , entriesCache );
934
955
}
@@ -1251,10 +1272,10 @@ private record ClassPathManifestEntry(Resource resource, @Nullable Resource alte
1251
1272
1252
1273
private static final String JARFILE_URL_PREFIX = ResourceUtils .JAR_URL_PREFIX + ResourceUtils .FILE_URL_PREFIX ;
1253
1274
1254
- static ClassPathManifestEntry of (File file ) throws MalformedURLException {
1275
+ static ClassPathManifestEntry of (File file , @ Nullable Boolean useCaches ) throws MalformedURLException {
1255
1276
String path = fixPath (file .getAbsolutePath ());
1256
- Resource resource = asJarFileResource (path );
1257
- Resource alternative = createAlternative (path );
1277
+ Resource resource = asJarFileResource (path , useCaches );
1278
+ Resource alternative = createAlternative (path , useCaches );
1258
1279
return new ClassPathManifestEntry (resource , alternative );
1259
1280
}
1260
1281
@@ -1274,18 +1295,22 @@ private static String fixPath(String path) {
1274
1295
* @param path the file path (with or without a leading slash)
1275
1296
* @return the alternative form or {@code null}
1276
1297
*/
1277
- private static @ Nullable Resource createAlternative (String path ) {
1298
+ private static @ Nullable Resource createAlternative (String path , @ Nullable Boolean useCaches ) {
1278
1299
try {
1279
1300
String alternativePath = path .startsWith ("/" ) ? path .substring (1 ) : "/" + path ;
1280
- return asJarFileResource (alternativePath );
1301
+ return asJarFileResource (alternativePath , useCaches );
1281
1302
}
1282
1303
catch (MalformedURLException ex ) {
1283
1304
return null ;
1284
1305
}
1285
1306
}
1286
1307
1287
- private static Resource asJarFileResource (String path ) throws MalformedURLException {
1288
- return new UrlResource (JARFILE_URL_PREFIX + path + ResourceUtils .JAR_URL_SEPARATOR );
1308
+ private static Resource asJarFileResource (String path , @ Nullable Boolean useCaches ) throws MalformedURLException {
1309
+ UrlResource resource = new UrlResource (JARFILE_URL_PREFIX + path + ResourceUtils .JAR_URL_SEPARATOR );
1310
+ if (useCaches != null ) {
1311
+ resource .setUseCaches (useCaches );
1312
+ }
1313
+ return resource ;
1289
1314
}
1290
1315
}
1291
1316
0 commit comments