解决springboot热部署和shiro缓存管理器冲突问题:Another unnamed CacheManager already exists in the same VM.

springboot热部署可以帮助我们在修改保存的时候自动重启项目,重新编译。

最近在springboot整合shiro框架的时候出现了热部署和shiro框架缓存冲突的问题,也就是重启项目的时候缓存信息还在内存,没有释放,整理了解决方案如下:

首先确定我们缓存文件的名字,依靠这个名字我们可以获取已创建的缓存对象。

解决springboot热部署和shiro缓存管理器冲突问题:Another unnamed CacheManager already exists in the same VM.

通过查看源码发现shiro的缓存其实是封装的ehcache的缓存框架:解决springboot热部署和shiro缓存管理器冲突问题:Another unnamed CacheManager already exists in the same VM.

但是EhCacheManager类并没有获取已经创建缓存实例的方法:而在net.sf.ehcache.CacheManger里有

解决springboot热部署和shiro缓存管理器冲突问题:Another unnamed CacheManager already exists in the same VM.

但是最终实例对象还是net.sf.ehcache.CacheManger。所以直接获取net.sf.ehcache.CacheManger对象判断即可,

按照之前缓存配置文件xml设置的name属性获取到实例对象

解决springboot热部署和shiro缓存管理器冲突问题:Another unnamed CacheManager already exists in the same VM.

 

缓存同时要求将对应的所有需要被缓存的javabean实体映射类都需要实现序列化接口

解决springboot热部署和shiro缓存管理器冲突问题:Another unnamed CacheManager already exists in the same VM.