DEV Community


Posted on

Clash Of ClassLoaders

In the world of code sometimes situations are like you are trying to build a Burj Khalifa but stuck at first basement only recently I came across with a similar situation where I was working on an OAuth project where I was dealing with all complex security-related stuff and assuming that soon I will inaugurate my Burj Khalifa but I was stuck between Clash of ClassLoaders .

In this article, I will explain about spring boot and apache ignite integration and how spring will override default java class loaders.
you can find spring-boot ignite implementation code on GitHub
I was caching one of the user object and its properties like roles in ignite cache as an object, so at the time of retrieval I am getting an exception like

java.lang.IllegalStateException: Cached value is not of required type [cacheName=boot_ignite_cache, key=user_data, val=com.boot.ignite.bootignite.dto.User@1ae2cec9, requiredType=class com.boot.ignite.bootignite.dto.User]
at org.apache.ignite.cache.spring.SpringCache.get( ~[ignite-spring-2.7.0.jar:2.7.0]

After spending ample amount of time I realized there is something serious problem and I have forgotten my basics and started reading about classloaders and soon I diagnosed the problem that spring is way more friendly then we thought . Spring is using something called restart classloaders The restart technology provided by Spring Boot works by using two classloaders.

Classes that do not change (for example, those from third-party jars) are loaded into a base classloader.Classes that you are actively developing are loaded into a restart classloader . When the application is restarted, the restart classloader is thrown away and a new one is created. This approach means that application restarts are typically much faster than “cold starts”, since the base classloader is already available and populated.

So my user object loaded by spring boot restart class loader and ignite object is loaded by default class loader both the class are different.
The solution is either not to use spring dev tools which nobody wants or either create a file and exclude the classes.

Thanks for reading !!! Keep growing Keep Going !!!

Top comments (0)