Skip to content

Commit 6cc6ea1

Browse files
committed
Make jar caching configurable through setUseCaches
Closes gh-34694
1 parent b73ca60 commit 6cc6ea1

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -206,6 +206,8 @@
206206
*/
207207
public 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();

spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -108,9 +108,11 @@ void encodedHashtagInPath() throws IOException {
108108
Path rootDir = Paths.get("src/test/resources/custom%23root").toAbsolutePath();
109109
URL root = new URL("file:" + rootDir + "/");
110110
resolver = new PathMatchingResourcePatternResolver(new DefaultResourceLoader(new URLClassLoader(new URL[] {root})));
111+
resolver.setUseCaches(false);
111112
assertExactFilenames("classpath*:scanned/*.txt", "resource#test1.txt", "resource#test2.txt");
112113
}
113114

115+
114116
@Nested
115117
class WithHashtagsInTheirFilenames {
116118

@@ -332,7 +334,7 @@ private String getPath(Resource resource) {
332334
// Tests fail if we use resource.getURL().getPath(). They would also fail on macOS when
333335
// using resource.getURI().getPath() if the resource paths are not Unicode normalized.
334336
//
335-
// On the JVM, all tests should pass when using resouce.getFile().getPath(); however,
337+
// On the JVM, all tests should pass when using resource.getFile().getPath(); however,
336338
// we use FileSystemResource#getPath since this test class is sometimes run within a
337339
// GraalVM native image which cannot support Path#toFile.
338340
//

0 commit comments

Comments
 (0)