zoukankan      html  css  js  c++  java
  • springboot项目中,ehcache报错,需要CacheManager单例的解决办法

    在新的springboot项目中,如果在mybatis中使用了ehcache后,再第二次使用ehcache,会提示错误

    Another CacheManager with same name 'default' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:

    原因是在ehcache2.5版本后,cachemanager必须是单例的,不允许多次创建。

    解决办法

    在config的配置文件中,新建一个bean,然后署名,在程序引用他时,标注这个name为新建的cachemanager

    配置代码

    @Configuration
    public class EhCacheConfig {
    
    //    @Bean
    //    public CacheManager getCacheManager() {
    //        
    //        return  CacheManager.create("classpath:ehcache.xml");
    //    }
        @Bean(name = "demo")
        public CacheManager getCacheManagerDemo() {
            return  CacheManager.create("classpath:ehcache2.xml");
        }
    }

    ehcache2.xml ( ehcache.xml文件为系统默认加载的额,这里必须另建一个xml文件,以示区分 )

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache name="demo"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">
    <!-- xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" -->
        <!-- 磁盘保存路径 -->
        <diskStore path="D:44testehcache" />
    
        <defaultCache maxElementsInMemory="1000"
            maxElementsOnDisk="10000000" eternal="false" overflowToDisk="true"
            timeToIdleSeconds="120" timeToLiveSeconds="120"
            diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU">
        </defaultCache>
         
       
        
    </ehcache>

    引用部分

    在service层

    @Cacheable(cacheManager = "demo")
        public List<User> queryAll() {
            // TODO Auto-generated method stub
            return this.userMapper.queryAll();
        }

    启动入口类

    @SpringBootApplication
    @MapperScan(basePackages = {"cn.taotao.dao"})
    @EnableCaching
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
  • 相关阅读:
    622 CircularQueue C#
    x盒子
    Cygwin、MinG、MSys区别与联系(转)
    Spring集成MyBatis完整示例
    mybatis学习 (五) POJO的映射文件
    mybatis学习(四)——config全局配置文件解析
    json字段为null时输出空字符串
    mybatis学习(一)不使用 XML 构建 SqlSessionFactory
    数据库 ----jdbc连接池的弊端
    Spring @Import注解 —— 导入资源
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/14547822.html
Copyright © 2011-2022 走看看