LinkageError with Spring Boot Developer tools

Customizing the restart classloader to prevent LinkageError when using the Spring Boot Developer tools.

While converting an existing Spring application to Spring Boot I came across a LinkageError:

Caused by: java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method "com.sun.proxy.$Proxy203.payBill(Lcom/example/Bill;)V" the class loader (instance of org/springframework/boot/devtools/restart/classloader/RestartClassLoader) of the current class, com/sun/proxy/$Proxy203, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for interface com/example/services/BillService have different Class objects for the type com/example/Bill used in the signature
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(
    at java.lang.Class.getConstructor0(
    at java.lang.Class.getConstructor(
    at java.lang.reflect.Proxy.newProxyInstance(
    at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(
    at org.springframework.aop.framework.ProxyFactory.getProxy(
    at org.springframework.amqp.remoting.client.AmqpProxyFactoryBean.afterPropertiesSet(
    ... 47 common frames omitted

It worked fine when I disabled restarting of the Developer tools. So I checked the documentation and found the section on how restarting works. It uses two different classloaders. A restart classloader for changing classes and a base classloader for classes that don’t.

The stack trace tells us that both classloaders are loading Class Bill. This is because BillService is from a shared library and uses Bill from my code. The base classloader loads both classes on startup. The restart classloader uses BillService from the base classloader and loads Bill again.

To prevent this you have to load the shared library with the restart classloader. Thus we customize the restart classloader to include the shared library:

Share ·