Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot Classloader and Class Overriding

DZone's Guide to

Spring Boot Classloader and Class Overriding

Want to temporarily override library classes with your own custom ones, but Spring Boot's classloader is making it tough? Here is one solution.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

This article explains the Spring Boot classloader (LaunchedURLClassLoader) and a way to temporarily override library classes with your custom ones. 

Just a Little Fix

Let’s say you found a bug in some third-party JAR your app uses. As a good scout, you fixed it and created a pull request with a solution. The pull request was merged, but the fix is critical for you and you can’t wait till next library release. Is using a library snapshot the only way? Wouldn’t it be great if there was a solution to temporarily override only a few particular classes?

As an imaginary example (follow the code), let’s say you found a bug in the SpringBootBanner class and already have a solution to fix the banner’s colors — SpringBootBanner fixed.

(I know we can easily define custom banners in Spring Boot, it’s just a useful example — it will be super easy to spot if the ‘fix’ is working or not)

So what can we do to have the solution work immediately? Let’s just take the class (with the package) and paste it into our project (src/main/java).

Now let’s run the app from the IDE and everything seems to work:

Working Fix in an IDE

Great! But the joy is premature… If you build the app and run it:

./gradlew build
cd build/libs
java -jar spring-boot-loader-play-0.0.1-SNAPSHOT.jar


The original banner is still being displayed, and this is not about a terminal not supporting ANSI colors.

The banner class (SpringBootBanner) was simply not overridden.

Not Working Fix When Running Jar

The difference is that when you launch the app from an IDE, you have two kinds of artifacts: classes and JARs. Classes are loaded before JARs, so even though you have two versions of a class (your fix in /src/main/java and original in spring-boot-2.0.0.M7.jar lib), only the fix will be loaded. (ClassLoaders don’t care about duplicates — the class that is found first is loaded).

Spring Boot ClassLoader

With JARs, the situation is harder. It’s a Spring Boot fat JAR with the structure below:

+--- spring-boot-loader-play-0.0.1-SNAPSHOT.jar
     +--- META-INF
     +--- BOOT-INF
     |    +--- classes                            # 1 - project classes
     |    |     +--- org.springframework.boot
     |    |     | \--- SpringBootBanner.class    # this is our fix
     |    |     | 
     |    |     +--- pl.dk.loaderplay
     |    |          \--- SpringBootLoaderApplication.class
     |    |
     |    +--- lib                                # 2 - nested jar libraries
     |          +--- javax.annotation-api-1.3.1
     |          +--- spring-boot-2.0.0.M7.jar     # original banner class inside
     |          \--- (...)
     |
     +--- org.springframework.boot.loader         # Spring Boot loader classes
          +--- JarLauncher.class
          +--- LaunchedURLClassLoader.class
          \--- (...)


So actually, it contains three types of entries:

  1. Project classes
  2. Nested JAR libraries
  3. Spring Boot loader classes

Both project classes (BOOT-INF/classes) and nested JARs (BOOT-INF/lib) are handled by the same classLoader, which, in turn, resides in the root of the JAR (org.springframework.boot.loader.LaunchedURLClassLoader).

One might expect that LaunchedURLClassLoader will load the class content before the lib content, but the loader seems not the have that preference.

LaunchedURLClassLoader extends java.net.URLClassLoader, which is created with a set of URLs that will be used for classloading. The URL might point to a location like a JAR archive or classes folder. When classloading, all of the resources specified by URLs will be traversed in the order the URLs were provided, and the first resource containing the searched class will be used.

So how are the URLs are provided to LaunchedURLClassLoader? The JAR archive is parsed from top to bottom, and when an archive is found, it’s added to the URL list.

In our example:

"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-boot-starter-2.0.0.M7.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-boot-autoconfigure-2.0.0.M7.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-boot-2.0.0.M7.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-boot-starter-logging-2.0.0.M7.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/javax.annotation-api-1.3.1.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-context-5.0.2.RELEASE.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-aop-5.0.2.RELEASE.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-beans-5.0.2.RELEASE.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-expression-5.0.2.RELEASE.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-core-5.0.2.RELEASE.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/snakeyaml-1.19.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/logback-classic-1.2.3.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/log4j-to-slf4j-2.10.0.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/jul-to-slf4j-1.7.25.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/spring-jcl-5.0.2.RELEASE.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/logback-core-1.2.3.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/slf4j-api-1.7.25.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/log4j-api-2.10.0.jar!/"
"jar:file:/Users/dk/spring-boot-loader-play/build/libs/spring-boot-loader-play-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/"


As we can see, /BOOT-INF/classes is the last entry on the list — far after /BOOT-INF/lib/spring-boot-2.0.0.M7.jar. So, in the search for SpringBootBanner.class, the version from the latter will be used — not an outcome we hoped for.

On our quest to figure out what we can do about it, it’s worth zooming into how classloaders work in hierarchies.

Basically, classLoaders form hierarchies — with every child loader having a reference to its parent. With LaunchedURLClassLoader being the youngest descendant in Spring Boot's case, we end up with a simple hierarchy like this:

+--- sun.misc.Launcher$ExtClassLoader     # loading classes /jre/lib/ext/
     +--- sun.misc.Launcher.Launcher$AppClassLoader    # loading classes from the root of the jar - spring-boot-loader-play-0.0.1-SNAPSHOT.jar
          +--- org.springframework.boot.loader.LaunchedUrlClassLoader    # loading classes from /BOOT-INF/lib/ & /BOOT-INF/classes/

In Spring Boot, when the class is about to be loaded, we always start with LaunchedUrlClassLoader, but the “parent first” rule applies. This means that the child loader will try to load a given class only if the parent doesn’t find it. 

First Idea: AppClassLoader

If LaunchedUrlClassLoader delegates classloading to AppClassLoader, then why not to use it to load our class before it’s loaded by LaunchedUrlClassLoader?

You might be tempted to simply do:

Thread.currentThread().getContextClassLoader()
        .getParent()
        .loadClass("org.springframework.boot.SpringBootBanner");


But this won’t work. Yes, Thread.currentThread().getContextClassLoader().getParent() gets us the correct AppClassLoader, but this one is designed to work with standard JARs — where classes (with packages) are placed in the root of the jar. So where AppClassLoader has no problems handling the org.springframework.boot.loader classes (see the JAR directory tree above), it will not find classes in BOOT-INF/classes.

Meanwhile...

Thread.currentThread().getContextClassLoader()
        .getParent()
        .loadClass("BOOT-INF/classes/org/springframework/boot/SpringBootBanner");


...won’t work either. Yes, the class will be found, but the package will not match the path.

It appears there is no other way than copying SpringBootBanner from BOOT-INF/classes/org/ into the root of the JAR. If we do that, there is no need to call AppClassLoader directly to load the class, as it will always have precedence before LaunchedUrlClassLoader.

This is easily done with this Gradle build:

bootJar {
    with copySpec {
        from "$buildDir/classes/java/main/org"
        into 'org'
    }
}


We can launch the app and find that SpringBootBanner was copied to the JAR root and AppClassLoader was used to load it, but it won’t work. The problem is that SpringBootBanner depends on other classes — loaded by the child LaunchedUrlClassLoader. One thing we forgot about classloader hierarchy is that classes loaded by parents don’t see classes loaded by children.
The “load by AppClassLoader” idea seems to be a dead end — but worry not. We will use that knowledge with our second attempt!

LaunchedUrlClassLoader: Resource Order

It appears that parent loaders are not an option and we are stuck with the last loader in the hierarchy — LaunchedUrlClassLoader. You might remember that LaunchedUrlClassLoader loads classes traversing nested resources in the order they were provided to it. So let’s try to manipulate the order so that the /BOOT-INF/classes/ resource is first — not last on the list.

With org.springframework.boot.loader.JarLauncher, this seems to be an easy task, as it provides:

protected void postProcessClassPathArchives(List<Archive> archives)


This method manipulates archives just before they are given to LaunchedUrlClassLoader.

So let’s write a custom launcher using this functionality:

public class ClassesFirstJarLauncher extends JarLauncher {

    @Override
    protected void postProcessClassPathArchives(List<Archive> archives)
            throws MalformedURLException {
        for (int i = archives.size() - 1; i >= 0; i--) {
            Archive archive = archives.get(i);
            if (archive.getUrl().getPath().endsWith("/classes!/")) {
                archives.remove(archive);
                archives.add(0, archive);
                break;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        new ClassesFirstJarLauncher().launch(args);
    }
}


A quick reminder is that JarLauncher is the class launching your Spring Boot app. Check any Spring Boot MANIFEST.MF, and you will find something like:

Manifest-Version: 1.0
Start-Class: pl.dk.loaderplay.SpringBootLoaderApplication
Main-Class: org.springframework.boot.loader.JarLauncher


Main-Class being the class with the main method launched when we do:

java -jar spring-boot-loader-play-0.0.1-SNAPSHOT.jar


JarLauncher must be loaded by AppClassLoader (LaunchedUrlClassLoader is not even loaded itself yet), and to do that, it must be placed in the root of the JAR. Let’s use the trick we learned before:

bootJar {
    with copySpec {
        from "$buildDir/classes/java/main/pl/dk/loaderplay/ClassesFirstJarLauncher.class"
        into 'pl/dk/loaderplay'
    }
}


What remained is to replace Main-Class in our MANIFEST.MF. Spring Boot Gradle Plugin provides a way to do it:

bootJar {
    manifest {
        attributes 'Main-Class': 'pl.dk.loaderplay.ClassesFirstJarLauncher'
    }
}


Unfortunately, when replacing Main-Class, the original Spring Boot loader/launcher classes are not copied to the root of the JAR — and we still need them. This is how Spring Boot Gradle Plugin works, and I have not found a way around it. (It happens because the plugin’s BootZipCopyAction decision whether or not to copy the loader files is based upon whether the original JarLauncher was used or not).

So changing Main-Class by bootJar configuration is of no use to us. One can try to change it in some other way. For me, it was enough to leave the original Main-Class in the manifesto and simply specify start class when launching the app.

java -cp spring-boot-loader-play-0.0.1-SNAPSHOT.jar \
pl.dk.loaderplay.ClassesFirstJarLauncher


When doing so, the class was finally overridden:

Working Fix When Running Jar

Quick Summary

To summarize shortly:

Goal

Override...

spring-boot-loader-play-0.0.1-SNAPSHOT.jar
/BOOT-INF/lib/spring-boot-2.0.0.M7.jar/org/springframework/boot/SpringBootBanner.class


...with:

spring-boot-loader-play-0.0.1-SNAPSHOT.jar
/BOOT-INF/classes/org/springframework/boot/bootSpringBootBanner.class


Steps

  1. Place the overriding SpringBootBanner in src/main/java
  2. Create a custom launcher ordering the resources from which classes are loaded — ClassesFirstJarLauncher
  3. Copy the launcher to root of the JAR via bootJar Gradle task
  4. Launch the archive specifying the launcher class:
    java -cp spring-boot-loader-play-0.0.1-SNAPSHOT.jar \
    pl.dk.loaderplay.ClassesFirstJarLauncher

 Again, you may check the code here.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,spring boot ,classloader ,class overriding ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}