11/*
2- * Copyright 2002-2024 the original author or authors.
2+ * Copyright 2002-2025 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
206206 */
207207public class PathMatchingResourcePatternResolver implements ResourcePatternResolver {
208208
209+ private static final Resource [] EMPTY_RESOURCE_ARRAY = {};
210+
209211 private static final Log logger = LogFactory .getLog (PathMatchingResourcePatternResolver .class );
210212
211213 /**
@@ -248,6 +250,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
248250
249251 private PathMatcher pathMatcher = new AntPathMatcher ();
250252
253+ private boolean useCaches = true ;
254+
251255
252256 /**
253257 * Create a {@code PathMatchingResourcePatternResolver} with a
@@ -315,6 +319,21 @@ public PathMatcher getPathMatcher() {
315319 return this .pathMatcher ;
316320 }
317321
322+ /**
323+ * Specify whether this resolver should use jar caches. Default is {@code true}.
324+ * <p>Switch this flag to {@code false} in order to avoid jar caching at the
325+ * {@link JarURLConnection} level.
326+ * <p>Note that {@link JarURLConnection#setDefaultUseCaches} can be turned off
327+ * independently. This resolver-level setting is designed to only enforce
328+ * {@code JarURLConnection#setUseCaches(false)} if necessary but otherwise
329+ * leaves the JVM-level default in place.
330+ * @since 6.1.19
331+ * @see JarURLConnection#setUseCaches
332+ */
333+ public void setUseCaches (boolean useCaches ) {
334+ this .useCaches = useCaches ;
335+ }
336+
318337
319338 @ Override
320339 public Resource getResource (String location ) {
@@ -338,7 +357,7 @@ public Resource[] getResources(String locationPattern) throws IOException {
338357 // all class path resources with the given name
339358 Collections .addAll (resources , findAllClassPathResources (locationPatternWithoutPrefix ));
340359 }
341- return resources .toArray (new Resource [ 0 ] );
360+ return resources .toArray (EMPTY_RESOURCE_ARRAY );
342361 }
343362 else {
344363 // Generally only look for a pattern after a prefix here,
@@ -371,7 +390,7 @@ protected Resource[] findAllClassPathResources(String location) throws IOExcepti
371390 if (logger .isTraceEnabled ()) {
372391 logger .trace ("Resolved class path location [" + path + "] to resources " + result );
373392 }
374- return result .toArray (new Resource [ 0 ] );
393+ return result .toArray (EMPTY_RESOURCE_ARRAY );
375394 }
376395
377396 /**
@@ -607,7 +626,7 @@ else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) {
607626 if (logger .isTraceEnabled ()) {
608627 logger .trace ("Resolved location pattern [" + locationPattern + "] to resources " + result );
609628 }
610- return result .toArray (new Resource [ 0 ] );
629+ return result .toArray (EMPTY_RESOURCE_ARRAY );
611630 }
612631
613632 /**
@@ -695,6 +714,9 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
695714
696715 if (con instanceof JarURLConnection jarCon ) {
697716 // Should usually be the case for traditional JAR files.
717+ if (!this .useCaches ) {
718+ jarCon .setUseCaches (false );
719+ }
698720 jarFile = jarCon .getJarFile ();
699721 jarFileUrl = jarCon .getJarFileURL ().toExternalForm ();
700722 JarEntry jarEntry = jarCon .getJarEntry ();
0 commit comments