springboot2+shiro+ehcache

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.class]: Unsatisfied dependency expressed through method 'cacheManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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:
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.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@5f61d894]
 

关于上述报错的原因和解决方案:

原因:shiro 2.5.0以后版本呢。cachemananger采用单例模式。自定义的时候。如果不指定名称会出现重复加载的问题

解决:

依赖如下:


 org.apache.shiro
 shiro-core
 1.4.0


 org.apache.shiro
 shiro-spring
 1.4.0


 org.apache.shiro
 shiro-ehcache
 1.4.0



 com.github.theborakompanioni
 thymeleaf-extras-shiro
 1.2.1
@Bean
public EhCacheManager ehCacheManager(){
    EhCacheManager cacheManager = new EhCacheManager();
    cacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
    return cacheManager;
}
@Bean
public SessionManager sessionManager(){
    DefaultSessionManager sessionManager = new DefaultSessionManager();
    sessionManager.setCacheManager(ehCacheManager());
    return sessionManager;
}

 

 @Bean(name = "securityManager")
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(realm());
        securityManager.setSessionManager(sessionManager());
        //记住我管理
        securityManager.setRememberMeManager(rememberMeManager());
//        securityManager.setCacheManager(ehCacheManager());
        return securityManager;
    }

 

@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
    EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();
    cacheManagerFactoryBean.setShared(true);
    return cacheManagerFactoryBean;
}

你可能感兴趣的:(web)