springboot(23)ehcache读取缓存ClassCastException

现象

本地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

分析

stackoverflow

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:

  1. Disable cache when you’re running your application in development
  2. Use a different cache manager (if you’re using Spring Boot 1.3, you could force a simple cache manager using the spring.cache.type property in application-dev.properties and enable the dev profile in your IDE)
  3. 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值