1

使用 spring 注释缓存时遇到错误

@Cacheable(key = "#pwId", value = { "pwId" })

应用程序上下文 xsd---->


 xmlns:cache='http://www.springframework.org/schema/cache
 http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache-3.1.xsd

应用上下文


 <cache:annotation-driven/>

<context:component-scan base-package="com.ample.mis"></context:component-scan>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
     p:cacheManager-ref="ehcache"/>
 <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
     p:configLocation="WEB-INF/ehcache.xml" 
     p:shared="true"/>

缓存服务类/方法


@Service("programService")
public class ProgramServiceImpl
@Cacheable(key = "#pwId", value = { "pwId" })
public PendingRep editProgram(String pwId) {
    return programDao.editProgram(pwId);
}
}

ehcache.xml


<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> 
<defaultCache
      maxElementsInMemory="1000"  
      eternal="true"  
      overflowToDisk="false" 
      memoryStoreEvictionPolicy="LFU" />

  <cache name="editProgram" 
      maxElementsInMemory="1000"  
      eternal="true" 
      overflowToDisk="false"
      memoryStoreEvictionPolicy="LFU" />

以及得到的错误


SEVERE: java.lang.IllegalArgumentException: Cannot find cache named [pwId] for CacheDefinition[public com.ample.mis.bean.gk.PendingRep com.ample.mis.service.gk.impl.ProgramServiceImpl.editProgram(java.lang.String)] caches=[pwId] | condition='' | key='#pwId'
javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: Cannot find cache named [pwId] for CacheDefinition[public com.ample.mis.bean.gk.PendingRep com.ample.mis.service.gk.impl.ProgramServiceImpl.editProgram(java.lang.String)] caches=[pwId] | condition='' | key='#pwId'
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIData.broadcast(UIData.java:911)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1255)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:334)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.ample.mis.util.common.SessionFilter.doFilter(SessionFilter.java:78)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我在应用程序上下文中使用正确的类(ehcache)吗?请帮助

提前致谢

4

3 回答 3

2

"pwId"在注释中用作缓存名称。所以你必须定义一个名为的缓存区域"pwId",而不是"editProgram".

您在 ehcache.xml 中的缓存区域定义应如下所示:

<cache name="pwId" 
      maxElementsInMemory="1000"  
      eternal="true" 
      overflowToDisk="false"
      memoryStoreEvictionPolicy="LFU" />
于 2013-06-24T12:37:06.340 回答
1

尝试

@Cacheable("pwId")

如果您的 xml 配置是:

<cache name="pwId" 
  maxElementsInMemory="1000"  
  eternal="true" 
  overflowToDisk="false"
  memoryStoreEvictionPolicy="LFU" />
于 2014-05-08T12:59:50.693 回答
0

尝试

@Cacheable(key = "#a0", value = { "pwId" })

反而

@Cacheable(key = "#pwId", value = { "pwId" })
于 2015-03-13T20:00:38.130 回答