-
Notifications
You must be signed in to change notification settings - Fork 41.5k
Description
If Spring Session Redis and Spring Boot Dev Tools are being used and a user defined class is persisted into HttpSession a ClassCastException will occur when trying to load it.
The problem is that Spring Session uses Spring Data Redis to load the class on each request. Spring Data Redis eventually uses DefaultDeserializer
which uses an ObjectInputStream
to load the class from the system ClassLoader
.
This means that the value restored from session has a class defined by the system ClassLoader
. The application itself loads the same class from Spring Boot's RestartClassLoader
. Since the two classes are loaded from different ClassLoader
s they cannot be cast from one to the other.
One possible solution is for Spring Redis to use a mechanism to customize the ObjectInputStream
ClassLoader
to use the ThreadLocal.getThread().getContextClassLoader()
. For example, it could use ConfigurableObjectInputStream
I believe one alternative is that if DefaultDeserializer
were loaded using RestartClassLoader
, then the ObjectInputStream
should use the RestartClassLoader
.
Related (possibly replaces this issue): SPR-13409