Skip to content

Commit 77cbab7

Browse files
committed
Make TomcatEmbeddedWebappClassLoader parallel capable
Closes gh-10477
1 parent 027c5a0 commit 77cbab7

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/tomcat/TomcatEmbeddedWebappClassLoader.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,28 @@
2121
import java.util.Collections;
2222
import java.util.Enumeration;
2323

24-
import org.apache.catalina.loader.WebappClassLoader;
24+
import org.apache.catalina.loader.ParallelWebappClassLoader;
2525
import org.apache.commons.logging.Log;
2626
import org.apache.commons.logging.LogFactory;
2727

2828
/**
29-
* Extension of Tomcat's {@link WebappClassLoader} that does not consider the
29+
* Extension of Tomcat's {@link ParallelWebappClassLoader} that does not consider the
3030
* {@link ClassLoader#getSystemClassLoader() system classloader}. This is required to
31-
* ensure that any custom context classloader is always used (as is the case with some
31+
* ensure that any custom context class loader is always used (as is the case with some
3232
* executable archives).
3333
*
3434
* @author Phillip Webb
3535
* @since 2.0.0
3636
*/
37-
public class TomcatEmbeddedWebappClassLoader extends WebappClassLoader {
37+
public class TomcatEmbeddedWebappClassLoader extends ParallelWebappClassLoader {
3838

3939
private static final Log logger = LogFactory
4040
.getLog(TomcatEmbeddedWebappClassLoader.class);
4141

42+
static {
43+
ClassLoader.registerAsParallelCapable();
44+
}
45+
4246
public TomcatEmbeddedWebappClassLoader() {
4347
super();
4448
}
@@ -58,14 +62,16 @@ public Enumeration<URL> findResources(String name) throws IOException {
5862
}
5963

6064
@Override
61-
public synchronized Class<?> loadClass(String name, boolean resolve)
65+
public Class<?> loadClass(String name, boolean resolve)
6266
throws ClassNotFoundException {
63-
Class<?> result = findExistingLoadedClass(name);
64-
result = (result == null ? doLoadClass(name) : result);
65-
if (result == null) {
66-
throw new ClassNotFoundException(name);
67+
synchronized (getClassLoadingLock(name)) {
68+
Class<?> result = findExistingLoadedClass(name);
69+
result = (result == null ? doLoadClass(name) : result);
70+
if (result == null) {
71+
throw new ClassNotFoundException(name);
72+
}
73+
return resolveIfNecessary(result, resolve);
6774
}
68-
return resolveIfNecessary(result, resolve);
6975
}
7076

7177
private Class<?> findExistingLoadedClass(String name) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/tomcat/TomcatEmbeddedWebappClassLoaderTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.util.zip.ZipEntry;
2929

3030
import org.apache.catalina.core.StandardContext;
31-
import org.apache.catalina.loader.WebappClassLoader;
31+
import org.apache.catalina.loader.ParallelWebappClassLoader;
3232
import org.apache.catalina.webresources.StandardRoot;
3333
import org.apache.catalina.webresources.WarResourceSet;
3434
import org.junit.Rule;
@@ -73,7 +73,7 @@ private void withWebappClassLoader(File war, ClassLoaderConsumer consumer)
7373
throws Exception {
7474
URLClassLoader parent = new URLClassLoader(
7575
new URL[] { new URL(webInfClassesUrlString(war)) }, null);
76-
try (WebappClassLoader classLoader = new TomcatEmbeddedWebappClassLoader(
76+
try (ParallelWebappClassLoader classLoader = new TomcatEmbeddedWebappClassLoader(
7777
parent)) {
7878
StandardContext context = new StandardContext();
7979
context.setName("test");

0 commit comments

Comments
 (0)