1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
206
206
*/
207
207
public class PathMatchingResourcePatternResolver implements ResourcePatternResolver {
208
208
209
+ private static final Resource [] EMPTY_RESOURCE_ARRAY = {};
210
+
209
211
private static final Log logger = LogFactory .getLog (PathMatchingResourcePatternResolver .class );
210
212
211
213
/**
@@ -248,6 +250,8 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
248
250
249
251
private PathMatcher pathMatcher = new AntPathMatcher ();
250
252
253
+ private boolean useCaches = true ;
254
+
251
255
252
256
/**
253
257
* Create a {@code PathMatchingResourcePatternResolver} with a
@@ -315,6 +319,21 @@ public PathMatcher getPathMatcher() {
315
319
return this .pathMatcher ;
316
320
}
317
321
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
+
318
337
319
338
@ Override
320
339
public Resource getResource (String location ) {
@@ -338,7 +357,7 @@ public Resource[] getResources(String locationPattern) throws IOException {
338
357
// all class path resources with the given name
339
358
Collections .addAll (resources , findAllClassPathResources (locationPatternWithoutPrefix ));
340
359
}
341
- return resources .toArray (new Resource [ 0 ] );
360
+ return resources .toArray (EMPTY_RESOURCE_ARRAY );
342
361
}
343
362
else {
344
363
// Generally only look for a pattern after a prefix here,
@@ -371,7 +390,7 @@ protected Resource[] findAllClassPathResources(String location) throws IOExcepti
371
390
if (logger .isTraceEnabled ()) {
372
391
logger .trace ("Resolved class path location [" + path + "] to resources " + result );
373
392
}
374
- return result .toArray (new Resource [ 0 ] );
393
+ return result .toArray (EMPTY_RESOURCE_ARRAY );
375
394
}
376
395
377
396
/**
@@ -607,7 +626,7 @@ else if (ResourceUtils.isJarURL(rootDirUrl) || isJarResource(rootDirResource)) {
607
626
if (logger .isTraceEnabled ()) {
608
627
logger .trace ("Resolved location pattern [" + locationPattern + "] to resources " + result );
609
628
}
610
- return result .toArray (new Resource [ 0 ] );
629
+ return result .toArray (EMPTY_RESOURCE_ARRAY );
611
630
}
612
631
613
632
/**
@@ -695,6 +714,9 @@ protected Set<Resource> doFindPathMatchingJarResources(Resource rootDirResource,
695
714
696
715
if (con instanceof JarURLConnection jarCon ) {
697
716
// Should usually be the case for traditional JAR files.
717
+ if (!this .useCaches ) {
718
+ jarCon .setUseCaches (false );
719
+ }
698
720
jarFile = jarCon .getJarFile ();
699
721
jarFileUrl = jarCon .getJarFileURL ().toExternalForm ();
700
722
JarEntry jarEntry = jarCon .getJarEntry ();
0 commit comments