当apache shiro跟spring cache结合使用时

如果双方都用ehcache作为缓冲库的话,对于cachemanger的设定会产生冲突,因为如果没有给shiro的EhCacheManager设定cachemanager的话,会创建一个默认名称的cachemanager,而spring的cache自动配置也同样会创建一个默认名称的cachemanager,由于ehcache2.5以后,不允许同一个jvm上存在两个同样名字的cachemanager,所以会报错。

解决的方法有两个,一个是自定义配置java文件,把名称改为另外一个,或者干脆就用同一个cachemanager。

下面是用同一个cachemanager时的配置方法。

@Bean(name = "ehcache")
	public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
		EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
		ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
		ehCacheManagerFactoryBean.setShared(true);
		return ehCacheManagerFactoryBean;
	}
	
	@Bean(name = "cacheManager")
	public EhCacheCacheManager ehCacheCacheManager(){
		EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
		ehCacheCacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
		return ehCacheCacheManager;
	}
	@Bean(name = "ehCacheManager")
	@DependsOn("lifecycleBeanPostProcessor")
	public EhCacheManager ehCacheManager(){
		EhCacheManager ehCacheManager = new EhCacheManager();
		ehCacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
		return ehCacheManager;
	}

相关推荐