【问题集】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.

你可能感兴趣的:(java)