Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.sf.ehcache.CacheManager]: Factory method 'ehCacheCacheManager' threw exception; nested exception is net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
报错信息描述
原因:org.springframework.beans.BeanInstantiationException: Failed to instantiate [net. jsf .ehcache.]:工厂方法’ehCacheCacheManager’抛出异常;嵌套异常是net.sf.ehcache.CacheException:另一个未命名的CacheManager已经存在于同一个虚拟机中。请在配置中为每个CacheManager提供唯一的名称,或者执行以下操作之一:
- Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
- Shutdown the earlier cacheManager before creating new one with same name.
1.使用一个CacheManager.create()静态工厂方法来重用具有相同名称的同一个CacheManager,或者在必要时创建一个 - 在创建同名的新缓存器之前,先关闭之前的缓存器。
原因
Ehcache 集成 shiro 后再集成spring cache后出现 CacheManager 冲突
解决方法
在ShiroConfig中添加以下代码
@Bean
@ConditionalOnMissingBean
public net.sf.ehcache.CacheManager ehCacheCacheManager() {
return CacheManager.create();
}
解决思路
产生这个问题根本的原因是,shiro每次早于EhCacheCacheConfiguration去构造对象,当shiro中已经构造了cacheMangaer时,后面再重复构造就会抛出异常。
使用@ConditionalOnMissingBean注解,它是修饰bean的一个注解,主要实现的是,当你的bean被注册之后,如果而注册相同类型的bean,就不会成功,它会保证你的bean只有一个,即你的实例只有一个。