【问题集】Springboot @Cacheable java.lang.ClassCastException: xxx cannot be cast to xxx

问题描述:
Springboot项目,当用到@Cacheable注解的方法时,不走缓存方法正常,当触发缓存时报错,错误如下:
java.lang.ClassCastException: xxx cannot be cast to xxx

很有意思,xxx不能转换为xxx,按常理说不通,A怎么不能cast成A呢,debug仔细检查了是否有拼错类名及不一致情况,确实xxx就是xxx,
怎么就不能cast呢?

google一通得知和热部署有关,和用到的spring-boot-devtools有关,和classloader有关,这样就说的通了。
看了许多帖子,都是比较直接的解决方案,把spring-boot-devtools注释掉,不要热部署了,这真是治病除根啊!!
可是以后怎么愉快的玩耍。

继续刨根

先说解决方案,以下是stackoverflow上提供的解决思路,个人觉得比较解决,比直接停用devTool好点

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 the spring.cache.type
property in application-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

解决方式:
1.开发环境禁用cache,怎么禁才能无缝的和其他环境衔接,要不拖泥带水。把 @EnableCaching注释掉,记得加个TODO,其他环境记得改回来。
2.用其他的cache manager,通过dev的配置文件将spring.cache.type=simple。我试了下还是有问题??
3.针对Devtools的局限性(下文有说),deserialize的时候设置classloader。有必要吗?为了一个dev工具写一票代码?

相关帖子看这里:
https://stackoverflow.com/questions/34577936/spring-boot-devtools-causing-classcastexception-while-getting-from-cache/41766003#41766003

原因文章了说了Devtools的局限,相关文档看这里。
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-known-restart-limitations

Restart functionality does not work well with objects that are
deserialized by using a standard ObjectInputStream. If you need to
deserialize data, you may need to use Spring’s
ConfigurableObjectInputStream in combination with
Thread.currentThread().getContextClassLoader().

Unfortunately, several third-party libraries deserialize without
considering the context classloader. If you find such a problem, you
need to request a fix with the original authors.

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,@Cacheable是一个注解,用于启用基于注解的缓存功能。通过在需要缓存的方法上添加@Cacheable注解,可以将方法的返回结果缓存起来,下次调用相同参数的方法时,直接从缓存中获取结果,提高了系统的性能和响应速度。 @Cacheable注解可以用在方法级别或类级别。在方法级别上使用@Cacheable注解时,可以指定缓存的名称和缓存的key。当调用被@Cacheable注解修饰的方法时,Spring会先检查缓存中是否存在相应的缓存数据,如果存在,则直接返回缓存数据;如果不存在,则执行方法体中的逻辑,并将返回结果存入缓存中。 使用@Cacheable注解需要在Spring Boot主程序类上添加@EnableCaching注解,以启用缓存功能。另外,还需要配置相应的缓存管理器,可以使用Redis等缓存技术来实现缓存功能。在Spring Boot中,可以使用Spring Data Redis作为缓存管理器。 总之,通过使用@Cacheable注解,可以方便地实现方法级别的缓存功能,提高系统的性能和响应速度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot缓存篇01——@Cacheable属性介绍和简单使用](https://blog.csdn.net/qq_41008818/article/details/112253215)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot 缓存之 @Cacheable介绍](https://blog.csdn.net/qq_50652600/article/details/122791156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值