Cannot cast variable name of the same class

I am implementing a workflow where each task is of Service Task type, and each service task is implemented as a ExternalTaskHandler correspondingly in the code. I use VariableMap to store and pass data among the external task handlers. I can store an object which is of a custom Java type as a value to the VariableMap, but passing the object inside VariableMap to the subsequent external task handler is not possible with the error “Cannot cast variable name …” when calling VariableMap.getValue(String key, Class targetClass) although the object and the target class were originated from the same class. I noticed that the class loaders are not the same that is causing the casting error, as follows:

  • Class loader of the object: jdk.internal.loader.ClassLoaders$AppClassLoader
  • Class loader of the target class: org.springframework.boot.devtools.restart.classloader.RestartClassLoader
    In my opinion, class loader of the object should be from spring boot devtools as well. Is the any solution to this?