|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.util.ArrayList; |
21 | | -import java.util.Arrays; |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.LinkedHashMap; |
22 | 23 | import java.util.List; |
| 24 | +import java.util.Map; |
23 | 25 |
|
24 | 26 | import org.springframework.beans.factory.FactoryBean; |
25 | 27 | import org.springframework.beans.factory.config.AbstractFactoryBean; |
@@ -72,17 +74,26 @@ public Class<? extends Resource[]> getObjectType() { |
72 | 74 | protected Resource[] createInstance() throws Exception { |
73 | 75 | List<Resource> scripts = new ArrayList<>(); |
74 | 76 | for (String location : this.locations) { |
75 | | - List<Resource> resources = new ArrayList<>( |
76 | | - Arrays.asList(this.resourcePatternResolver.getResources(location))); |
77 | | - resources.sort((r1, r2) -> { |
| 77 | + Resource[] resources = this.resourcePatternResolver.getResources(location); |
| 78 | + |
| 79 | + // Cache URLs to avoid repeated I/O during sorting |
| 80 | + Map<Resource, String> urlCache = new LinkedHashMap<>(resources.length); |
| 81 | + for (Resource resource : resources) { |
78 | 82 | try { |
79 | | - return r1.getURL().toString().compareTo(r2.getURL().toString()); |
| 83 | + urlCache.put(resource, resource.getURL().toString()); |
80 | 84 | } |
81 | 85 | catch (IOException ex) { |
82 | | - return 0; |
| 86 | + throw new IllegalStateException( |
| 87 | + "Failed to resolve URL for resource [" + resource + |
| 88 | + "] from location pattern [" + location + "]", ex); |
83 | 89 | } |
84 | | - }); |
85 | | - scripts.addAll(resources); |
| 90 | + } |
| 91 | + |
| 92 | + // Sort using cached URLs |
| 93 | + List<Resource> sortedResources = new ArrayList<>(urlCache.keySet()); |
| 94 | + sortedResources.sort(Comparator.comparing(urlCache::get)); |
| 95 | + |
| 96 | + scripts.addAll(sortedResources); |
86 | 97 | } |
87 | 98 | return scripts.toArray(new Resource[0]); |
88 | 99 | } |
|
0 commit comments