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);
        }
    
    }
  • 相关阅读:
    安装包报错2503解决方法
    js中return;、return true、return false;区别
    图片添加border 不占用图片的大小
    效果网址http://sc.chinaz.com/tag_jiaoben/tupianlunbo.html
    兼容ie8 rgba()用法
    html使用css让文字多行超出部分用省略号三个点显示的方法案例
    MyCat02--MyCat入门无ZK实践
    MyCat01--基础
    MySQL/MariaDB导入(load data infile)导出(select into outfile)
    MariaDB/Mysql临时表
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/14547822.html
Copyright © 2011-2022 走看看