Skip to content

Commit 939b66f

Browse files
tsachevwilkinsona
authored andcommitted
Use fast exceptions in findResource(s)
Some libraries like aspectj are using findResource to see the raw bytecode of a class. It will even call findResource for every method of every class of beans that are post processed. This can be significant performance hit on startup when LaunchedURLClassLoader and there are a lot of nested jars. See gh-3640 Fixes gh-4557
1 parent 0214fe4 commit 939b66f

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/LaunchedURLClassLoader.java

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ public URL findResource(String name) {
7676
if (name.equals("") && hasURLs()) {
7777
return getURLs()[0];
7878
}
79-
return super.findResource(name);
79+
Handler.setUseFastConnectionExceptions(true);
80+
try {
81+
return super.findResource(name);
82+
}
83+
finally {
84+
Handler.setUseFastConnectionExceptions(false);
85+
}
8086
}
8187
catch (IllegalArgumentException ex) {
8288
return null;
@@ -88,7 +94,13 @@ public Enumeration<URL> findResources(String name) throws IOException {
8894
if (name.equals("") && hasURLs()) {
8995
return Collections.enumeration(Arrays.asList(getURLs()));
9096
}
91-
return super.findResources(name);
97+
Handler.setUseFastConnectionExceptions(true);
98+
try {
99+
return super.findResources(name);
100+
}
101+
finally {
102+
Handler.setUseFastConnectionExceptions(false);
103+
}
92104
}
93105

94106
private boolean hasURLs() {
@@ -100,33 +112,8 @@ public Enumeration<URL> getResources(String name) throws IOException {
100112
if (this.rootClassLoader == null) {
101113
return findResources(name);
102114
}
103-
104-
final Enumeration<URL> rootResources = this.rootClassLoader.getResources(name);
105-
final Enumeration<URL> localResources = findResources(name);
106-
107-
return new Enumeration<URL>() {
108-
109-
@Override
110-
public boolean hasMoreElements() {
111-
try {
112-
Handler.setUseFastConnectionExceptions(true);
113-
return rootResources.hasMoreElements()
114-
|| localResources.hasMoreElements();
115-
}
116-
finally {
117-
Handler.setUseFastConnectionExceptions(false);
118-
}
119-
}
120-
121-
@Override
122-
public URL nextElement() {
123-
if (rootResources.hasMoreElements()) {
124-
return rootResources.nextElement();
125-
}
126-
return localResources.nextElement();
127-
}
128-
129-
};
115+
return new ResourceEnumeration(this.rootClassLoader.getResources(name),
116+
findResources(name));
130117
}
131118

132119
/**
@@ -162,6 +149,7 @@ private Class<?> doLoadClass(String name) throws ClassNotFoundException {
162149
}
163150
}
164151
catch (Exception ex) {
152+
165153
}
166154

167155
// 2) Try to find locally
@@ -171,6 +159,7 @@ private Class<?> doLoadClass(String name) throws ClassNotFoundException {
171159
return cls;
172160
}
173161
catch (Exception ex) {
162+
174163
}
175164

176165
// 3) Use standard loading
@@ -265,4 +254,41 @@ public Object getLock(LaunchedURLClassLoader classLoader, String className) {
265254

266255
}
267256

268-
}
257+
/**
258+
* {@link Enumeration} implementation used for {@code getResources()}.
259+
*/
260+
private static class ResourceEnumeration implements Enumeration<URL> {
261+
262+
private final Enumeration<URL> rootResources;
263+
264+
private final Enumeration<URL> localResources;
265+
266+
ResourceEnumeration(Enumeration<URL> rootResources,
267+
Enumeration<URL> localResources) {
268+
this.rootResources = rootResources;
269+
this.localResources = localResources;
270+
}
271+
272+
@Override
273+
public boolean hasMoreElements() {
274+
try {
275+
Handler.setUseFastConnectionExceptions(true);
276+
return this.rootResources.hasMoreElements()
277+
|| this.localResources.hasMoreElements();
278+
}
279+
finally {
280+
Handler.setUseFastConnectionExceptions(false);
281+
}
282+
}
283+
284+
@Override
285+
public URL nextElement() {
286+
if (this.rootResources.hasMoreElements()) {
287+
return this.rootResources.nextElement();
288+
}
289+
return this.localResources.nextElement();
290+
}
291+
292+
}
293+
294+
}

0 commit comments

Comments
 (0)