
7

springboot(25)ehcache读取缓存ClassCastException
source link: https://wakzz.cn/2018/09/03/springboot/(25)ehcache%E8%AF%BB%E5%8F%96%E7%BC%93%E5%AD%98ClassCastException/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

springboot(25)ehcache读取缓存ClassCastException
祈雨的博客
2018-09-03
本地IDE使用redis+ehcache缓存时,读取缓存抛出如下异常。明明是用一个类型,却发生A cannot be cast to A
的ClassCastException的异常,实在是令人摸不着头脑。
java.lang.ClassCastException: com.cardinfolink.saber.marketing.model.BenefitRecord cannot be cast to com.cardinfolink.saber.marketing.model.BenefitRecord
This is a known limitation of Devtools. When the cache entry is deserialized, the object is not attached to the proper classloader.
There are various ways you can fix this issue:
- Disable cache when you’re running your application in development
- Use a different cache manager (if you’re using Spring Boot 1.3, you could force a
simple
cache manager using thespring.cache.type
property inapplication-dev.properties
and enable the dev profile in your IDE)- Configure memcached (and things that are cached) to run in the application classloader. I wouldn’t recommend that option since the two first above are much easier to implement
当使用SpringBoot的DevTools用于热加载时,首先类加载器为A,对象被序列化到缓存。修改代码导致热加载后,DevTools重新创建了类加载器B。然后出现错误的类加载器加载对象导致A cannot be cast to A
的异常。
最简单无脑的方式就是删除DevTools的依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK