@@ -260,7 +260,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
260260
261261 private PathMatcher pathMatcher = new AntPathMatcher ();
262262
263- private boolean useCaches = true ;
263+ @ Nullable
264+ private Boolean useCaches ;
264265
265266 private final Map <String , Resource []> rootDirCache = new ConcurrentHashMap <>();
266267
@@ -342,10 +343,12 @@ public PathMatcher getPathMatcher() {
342343 * the {@link JarURLConnection} level as well as within this resolver instance.
343344 * <p>Note that {@link JarURLConnection#setDefaultUseCaches} can be turned off
344345 * 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}.
347349 * @since 6.1.19
348350 * @see JarURLConnection#setUseCaches
351+ * @see UrlResource#setUseCaches
349352 * @see #clearCache()
350353 */
351354 public void setUseCaches (boolean useCaches ) {
@@ -355,7 +358,11 @@ public void setUseCaches(boolean useCaches) {
355358
356359 @ Override
357360 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 ;
359366 }
360367
361368 @ Override
@@ -473,20 +480,27 @@ protected Resource convertClassLoaderURL(URL url) {
473480 }
474481 }
475482 else {
483+ UrlResource resource = null ;
476484 String urlString = url .toString ();
477485 String cleanedPath = StringUtils .cleanPath (urlString );
478486 if (!cleanedPath .equals (urlString )) {
479487 // Prefer cleaned URL, aligned with UrlResource#createRelative(String)
480488 try {
481489 // Retain original URL instance, potentially including custom URLStreamHandler.
482- return new UrlResource (new URL (url , cleanedPath ));
490+ resource = new UrlResource (new URL (url , cleanedPath ));
483491 }
484492 catch (MalformedURLException ex ) {
485493 // Fallback to regular URL construction below...
486494 }
487495 }
488496 // 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 ;
490504 }
491505 }
492506
@@ -505,6 +519,9 @@ protected void addAllClassLoaderJarRoots(@Nullable ClassLoader classLoader, Set<
505519 UrlResource jarResource = (ResourceUtils .URL_PROTOCOL_JAR .equals (url .getProtocol ()) ?
506520 new UrlResource (url ) :
507521 new UrlResource (ResourceUtils .JAR_URL_PREFIX + url + ResourceUtils .JAR_URL_SEPARATOR ));
522+ if (this .useCaches != null ) {
523+ jarResource .setUseCaches (this .useCaches );
524+ }
508525 if (jarResource .exists ()) {
509526 result .add (jarResource );
510527 }
@@ -556,7 +573,7 @@ protected void addClassPathManifestEntries(Set<Resource> result) {
556573 Set <ClassPathManifestEntry > entries = this .manifestEntriesCache ;
557574 if (entries == null ) {
558575 entries = getClassPathManifestEntries ();
559- if (this .useCaches ) {
576+ if (this .useCaches == null || this . useCaches ) {
560577 this .manifestEntriesCache = entries ;
561578 }
562579 }
@@ -577,7 +594,7 @@ private Set<ClassPathManifestEntry> getClassPathManifestEntries() {
577594 try {
578595 File jar = new File (path ).getAbsoluteFile ();
579596 if (jar .isFile () && seen .add (jar )) {
580- manifestEntries .add (ClassPathManifestEntry .of (jar ));
597+ manifestEntries .add (ClassPathManifestEntry .of (jar , this . useCaches ));
581598 manifestEntries .addAll (getClassPathManifestEntriesFromJar (jar ));
582599 }
583600 }
@@ -616,7 +633,7 @@ private Set<ClassPathManifestEntry> getClassPathManifestEntriesFromJar(File jar)
616633 }
617634 File candidate = new File (parent , path );
618635 if (candidate .isFile () && candidate .getCanonicalPath ().contains (parent .getCanonicalPath ())) {
619- manifestEntries .add (ClassPathManifestEntry .of (candidate ));
636+ manifestEntries .add (ClassPathManifestEntry .of (candidate , this . useCaches ));
620637 }
621638 }
622639 }
@@ -710,7 +727,7 @@ else if (commonPrefix.equals(rootDirPath)) {
710727 if (rootDirResources == null ) {
711728 // Lookup for specific directory, creating a cache entry for it.
712729 rootDirResources = getResources (rootDirPath );
713- if (this .useCaches ) {
730+ if (this .useCaches == null || this . useCaches ) {
714731 this .rootDirCache .put (rootDirPath , rootDirResources );
715732 }
716733 }
@@ -729,7 +746,11 @@ else if (commonPrefix.equals(rootDirPath)) {
729746 if (resolvedUrl != null ) {
730747 rootDirUrl = resolvedUrl ;
731748 }
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 ;
733754 }
734755 if (rootDirUrl .getProtocol ().startsWith (ResourceUtils .URL_PROTOCOL_VFS )) {
735756 result .addAll (VfsResourceMatchingDelegate .findMatchingResources (rootDirUrl , subPattern , getPathMatcher ()));
@@ -865,8 +886,8 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
865886
866887 if (con instanceof JarURLConnection jarCon ) {
867888 // 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 );
870891 }
871892 try {
872893 jarFile = jarCon .getJarFile ();
@@ -931,7 +952,7 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
931952 }
932953 }
933954 }
934- if (this .useCaches ) {
955+ if (this .useCaches == null || this . useCaches ) {
935956 // Cache jar entries in TreeSet for efficient searching on re-encounter.
936957 this .jarEntriesCache .put (jarFileUrl , entriesCache );
937958 }
@@ -1257,10 +1278,10 @@ private record ClassPathManifestEntry(Resource resource, @Nullable Resource alte
12571278
12581279 private static final String JARFILE_URL_PREFIX = ResourceUtils .JAR_URL_PREFIX + ResourceUtils .FILE_URL_PREFIX ;
12591280
1260- static ClassPathManifestEntry of (File file ) throws MalformedURLException {
1281+ static ClassPathManifestEntry of (File file , @ Nullable Boolean useCaches ) throws MalformedURLException {
12611282 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 );
12641285 return new ClassPathManifestEntry (resource , alternative );
12651286 }
12661287
@@ -1281,18 +1302,22 @@ private static String fixPath(String path) {
12811302 * @return the alternative form or {@code null}
12821303 */
12831304 @ Nullable
1284- private static Resource createAlternative (String path ) {
1305+ private static Resource createAlternative (String path , @ Nullable Boolean useCaches ) {
12851306 try {
12861307 String alternativePath = path .startsWith ("/" ) ? path .substring (1 ) : "/" + path ;
1287- return asJarFileResource (alternativePath );
1308+ return asJarFileResource (alternativePath , useCaches );
12881309 }
12891310 catch (MalformedURLException ex ) {
12901311 return null ;
12911312 }
12921313 }
12931314
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 ;
12961321 }
12971322 }
12981323
0 commit comments