在coding中使用了devtools热启动,结果每次保存代码都会报错:
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@13ceee8c]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRealm' defined in class path resource [cn/mirak/framework/config/ShiroConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [cn.mirak.framework.shiro.realm.UserRealm]: Factory method 'userRealm' threw exception; nested exception is org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Another CacheManager with same name 'em' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
由
Another CacheManager with same name 'em' already exists in the same VM
可知已存在一个名为em的CacheManager,遂百度,找到net.sf.ehcache.CacheManager的getCacheManager(Java.lang.String)方法可以按名称获取CacheManager ,但我用的是org.apache.shiro.cache.ehcache.EhCacheManage,不能直接转换类型.
于是看org.apache.shiro.cache.ehcache.EhCacheManager的源码,发现有这样个属性和这样个方法
/**
* The EhCache cache manager used by this implementation to create caches.
*/
protected net.sf.ehcache.CacheManager manager;
/**
* Sets the wrapped Ehcache {@link net.sf.ehcache.CacheManager CacheManager} instance.
*
* @param manager the wrapped Ehcache {@link net.sf.ehcache.CacheManager CacheManager} instance.
*/
public void setCacheManager(net.sf.ehcache.CacheManager manager) {
this.manager = manager;
}
原来最终用的还是
net.sf.ehcache.CacheManager,思路来了
代码如下
@Bean
public EhCacheManager getEhCacheManager()
{
net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.getCacheManager("em");
EhCacheManager em = new EhCacheManager();
if(ObjectUtils.isEmpty(cacheManager)) {
em.setCacheManagerConfigFile("classpath:ehcache/ehcache-shiro.xml");
return em;
} else {
em.setCacheManager(cacheManager);
return em;
}
}
再次保存,问题解决