Skip to content

Commit 6bd7f02

Browse files
committed
Avoid double exists() call for common resource resolution
See gh-30369 See gh-18990
1 parent 70d9f7c commit 6bd7f02

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

spring-context/src/main/java/org/springframework/context/support/ReloadableResourceBundleMessageSource.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -25,7 +25,6 @@
2525
import java.util.List;
2626
import java.util.Locale;
2727
import java.util.Map;
28-
import java.util.Objects;
2928
import java.util.Properties;
3029
import java.util.concurrent.ConcurrentHashMap;
3130
import java.util.concurrent.ConcurrentMap;
@@ -431,7 +430,7 @@ protected PropertiesHolder refreshProperties(String filename, @Nullable Properti
431430
long refreshTimestamp = (getCacheMillis() < 0 ? -1 : System.currentTimeMillis());
432431

433432
Resource resource = resolveResource(filename);
434-
if (resource.exists()) {
433+
if (resource != null) {
435434
long fileTimestamp = -1;
436435
if (getCacheMillis() >= 0) {
437436
// Last-modified timestamp of file will just be read if caching with timeout.
@@ -508,18 +507,18 @@ protected PropertiesHolder refreshProperties(String filename, @Nullable Properti
508507
* {@link PropertiesPersister#load(Properties, InputStream) load} methods
509508
* for other types of resources.
510509
* @param filename the bundle filename (basename + Locale)
511-
* @return the {@code Resource} to use
510+
* @return the {@code Resource} to use, or {@code null} if none found
512511
* @since 6.1
513512
*/
513+
@Nullable
514514
protected Resource resolveResource(String filename) {
515-
Resource resource = null;
516515
for (String fileExtension : this.fileExtensions) {
517-
resource = this.resourceLoader.getResource(filename + fileExtension);
516+
Resource resource = this.resourceLoader.getResource(filename + fileExtension);
518517
if (resource.exists()) {
519518
return resource;
520519
}
521520
}
522-
return Objects.requireNonNull(resource);
521+
return null;
523522
}
524523

525524
/**

spring-context/src/test/java/org/springframework/context/support/ResourceBundleMessageSourceTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ protected void doTestMessageAccess(
126126
if (alwaysUseMessageFormat) {
127127
pvs.add("alwaysUseMessageFormat", Boolean.TRUE);
128128
}
129-
Class<?> clazz = reloadable ?
130-
(Class<?>) ReloadableResourceBundleMessageSource.class : ResourceBundleMessageSource.class;
129+
Class<?> clazz = reloadable ? ReloadableResourceBundleMessageSource.class : ResourceBundleMessageSource.class;
131130
ac.registerSingleton("messageSource", clazz, pvs);
132131
ac.refresh();
133132

0 commit comments

Comments
 (0)