java web使用@Cacheable配置,总是报错

sfasdfasdfasf 发布于 2018/03/21 14:50
阅读 1K+
收藏 0

按照网上流程一步步配置spring缓存,运行项目还是报错,什么情况?

 

第一步:spring-servlet.xml 添加如下声明

  1. <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->   
  2.     <cache:annotation-driven cache-manager="cacheManager" />  
  3.       
  4.     <!-- cacheManager工厂类,指定ehcache.xml的位置 -->     
  5.     <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
  6.         <property name="configLocation"  value="classpath:cache/ehcache.xml"/>    
  7.     </bean>  
  8.       
  9.     <!-- 声明cacheManager -->    
  10.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
  11.         <property name="cacheManager" ref="cacheManagerFactory"/>  
  12.     </bean>  

第二步:创建 ehcache.xml

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"  
  4.     updateCheck="false" dynamicConfig="false" monitoring="autodetect">    
  5.     <diskStore path="java.io.tmpdir" />  
  6.     <!--  
  7.         diskStore path:用来配置磁盘缓存使用的物理路径  
  8.         name:   缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)  
  9.         eternal="false"   元素是否永恒,如果是就永不过期(必须设置)  
  10.         maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大   
  11.         maxElementsInMemory="1000" 内存缓存中最多可以存放的元素数量(必须设置)  
  12.         timeToIdleSeconds="0"   导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0  
  13.         timeToLiveSeconds="600" 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期  
  14.         overflowToDisk="false"  当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)  
  15.         diskPersistent="false"  磁盘缓存在VM重新启动时是否保持(默认为false)  
  16.         diskExpiryThreadIntervalSeconds="100" 磁盘失效线程运行时间间隔,默认是120秒  
  17.         memoryStoreEvictionPolicy="LFU" 内存存储与释放策略.当达到maxElementsInMemory时  
  18.                共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)默认使用"最近使用"策略  
  19.     -->  
  20.     <defaultCache    
  21.         eternal="false"  
  22.         maxElementsInMemory="3000"    
  23.         timeToIdleSeconds="3600"    
  24.         timeToLiveSeconds="0"    
  25.         overflowToDisk="true"    
  26.         diskPersistent="false"    
  27.         diskExpiryThreadIntervalSeconds="100"    
  28.         memoryStoreEvictionPolicy="LRU"/>  
  29.   
  30.     <cache name="propConfigCache"    
  31.         eternal="false"  
  32.         maxElementsInMemory="3000"    
  33.         overflowToDisk="true"  
  34.         timeToIdleSeconds="0"    
  35.         timeToLiveSeconds="1440"   
  36.         memoryStoreEvictionPolicy="LFU"/>  
  37.           
  38.     <cache name="workTimeCache"    
  39.         eternal="false"  
  40.         maxElementsInMemory="3000"    
  41.         overflowToDisk="true"  
  42.         timeToIdleSeconds="0"    
  43.         timeToLiveSeconds="1440"   
  44.         memoryStoreEvictionPolicy="LFU"/>  
  45.       
  46.     <cache name="threeInOneCache"    
  47.         eternal="false"  
  48.         maxElementsInMemory="3000"    
  49.         overflowToDisk="true"  
  50.         timeToIdleSeconds="0"    
  51.         timeToLiveSeconds="1440"   
  52.         memoryStoreEvictionPolicy="LFU"/>  
  53.       
  54.     <cache name="transferCache"    
  55.         eternal="false"  
  56.         maxElementsInMemory="1000"    
  57.         overflowToDisk="true"  
  58.         timeToIdleSeconds="0"    
  59.         timeToLiveSeconds="1440"   
  60.         memoryStoreEvictionPolicy="LFU"/>  
  61.           
  62.     <cache name="threeInOneFavCache"    
  63.         eternal="false"  
  64.         maxElementsInMemory="3000"    
  65.         overflowToDisk="true"  
  66.         timeToIdleSeconds="0"    
  67.         timeToLiveSeconds="1440"   
  68.         memoryStoreEvictionPolicy="LFU"/>  
  69.           
  70.     <cache name="reserveTimeCache"    
  71.         eternal="false"  
  72.         maxElementsInMemory="3000"    
  73.         overflowToDisk="true"  
  74.         timeToIdleSeconds="0"    
  75.         timeToLiveSeconds="1440"   
  76.         memoryStoreEvictionPolicy="LFU"/>  
  77.           
  78.     <cache name="mqServerNameCache"    
  79.         eternal="false"  
  80.         maxElementsInMemory="3000"    
  81.         overflowToDisk="true"  
  82.         timeToIdleSeconds="0"    
  83.         timeToLiveSeconds="1440"   
  84.         memoryStoreEvictionPolicy="LFU"/>  
  85.           
  86.     <cache name="schWorkTimeCache"    
  87.         eternal="false"  
  88.         maxElementsInMemory="3000"    
  89.         overflowToDisk="true"  
  90.         timeToIdleSeconds="0"    
  91.         timeToLiveSeconds="1440"   
  92.         memoryStoreEvictionPolicy="LFU"/>  
  93.           
  94. </ehcache></span> 

但是运行项目还是报错:信息如下

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.
 

翻译:

在同一个虚拟机已经存在另一个未命名的缓存管理器。请提供每个CacheManager独特的名字在配置或做下列:

1。用一个CacheManager。create()静态工厂方法来重用相同的缓存管理器具有相同的名称或创建一个必要的

2。关机前早CacheManager相同名字的创造新的一。

可是我没有在哪里创建过CacheManager啊~~~~

 

更新:

看网上解决方案是加一句:<property name="shared" value="true"/>

但是我加了后 shared报红,说明没有这个属性。

加载中
0
刘东帅
刘东帅

<property name="shared" value="true"/>添加到<property name="configLocation"  value="classpath:cache/ehcache.xml"/>  下面

sfasdfasdfasf
sfasdfasdfasf
好了 谢谢 厉害厉害!!!!
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部