我在我的应用程序中使用Ehcache + Spring + Hibernte这里有一些详细信息 低于例外
引起:com.googlecode.ehcache.annotations.CacheNotFoundException: 无法找到缓存' com.ccc.spring.model.Validuserrole'在 com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getCache(CacheAttributeSourceImpl.java:223) 在 com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.parseCacheableAnnotation(CacheAttributeSourceImpl.java:325) 在 com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.findMethodAttribute(CacheAttributeSourceImpl.java:268) 在 com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.computeMethodAttribute(CacheAttributeSourceImpl.java:242) 在 com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getMethodAttribute(CacheAttributeSourceImpl.java:167) 在 com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getAdviceType(CacheAttributeSourceImpl.java:122) 在 com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut.matches(CacheStaticMethodMatcherPointcut.java:46) 在 org.springframework.aop.support.AopUtils.canApply(AopUtils.java:225) 在 org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262) 在 org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294) 在 org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118) 在 org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88) 在 org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69) 在 org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:376) 在 org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:339) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1558) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ......还有21个
这是我的ehcache.xml文件
<ehcache>
<defaultCache maxElementsInMemory="100" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="200" />
<cache name="com.ccc.spring.model.Validuserrole" maxElementsInMemory="100"
eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="2000" />
</ehcache>
applicationContext.xml文件条目
<ehcache:annotation-driven />
<ehcache:config cache-manager="cacheManager">
<ehcache:evict-expired-elements
interval="60" />
</ehcache:config>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="/WEB-INF/ehcache.xml" />
</bean>
<context:component-scan base-package="com.ccc.spring" />
在DAO我正在使用下面的代码
@Cacheable(cacheName="com.ccc.spring.model.Validuserrole")
public List<Validuserrole> getRoleList() {
List list = getSessionFactory().getCurrentSession()
.createQuery("from Validuserrole where active = ?")
.setParameter(0, true).list();
return list;
}
但仍未找到缓存。
修改
@Cacheable( value = { "com.ccc.spring.model.Validuserrole" })
public List<Validuserrole> getRoleList() {
List list = null;
try{
list = getSessionFactory().getCurrentSession()
.createQuery("from Validuserrole ").list();
}
catch(Exception e){
e.printStackTrace();
}
return list;
}
另一个编辑
Hibernate: select validuserr0_.roleid as roleid15_, validuserr0_.active as active15_, validuserr0_.createdon as createdon15_, validuserr0_.roledescription as roledesc4_15_ from subodhc_ccc.validuserrole validuserr0_
Hibernate: select user0_.userid as userid11_, user0_.aboutme as aboutme11_, user0_.addressid as addressid11_, user0_.createdon as createdon11_, user0_.dikshadate as dikshadate11_, user0_.dob as dob11_, user0_.emailid as emailid11_, user0_.firstname as firstname11_, user0_.gender as gender11_, user0_.lastmodifiedby as lastmodi9_11_, user0_.lastmodifiedon as lastmod10_11_, user0_.lastname as lastname11_, user0_.middlename as middlename11_, user0_.mobilenumber as mobilen13_11_, user0_.password as password11_, user0_.phonenumber as phonenu15_11_, user0_.username as username11_, user0_.statusid as statusid11_, user0_.roleid as roleid11_ from subodhc_ccc.user user0_ where user0_.statusid=2 and user0_.roleid=4
Hibernate: select country0_.CountryId as CountryId2_, country0_.CountryName as CountryN2_2_, country0_.LastUpdate as LastUpdate2_ from subodhc_ccc.country country0_
Hibernate: select validuserr0_.roleid as roleid15_, validuserr0_.active as active15_, validuserr0_.createdon as createdon15_, validuserr0_.roledescription as roledesc4_15_ from subodhc_ccc.validuserrole validuserr0_
Hibernate: select country0_.CountryId as CountryId2_, country0_.CountryName as CountryN2_2_, country0_.LastUpdate as LastUpdate2_ from subodhc_ccc.country country0_
答案 0 :(得分:2)
问题是您使用错误的注释进行Spring + EhCache集成。您需要使用org.springframework.cache.annotation.Cacheable
代替com.googlecode.ehcache.annotations.Cacheable
。
查看this和this,了解有关如何使用Spring的缓存支持(在Spring 3.1中引入)和EhCache作为缓存提供程序的更多信息
答案 1 :(得分:0)
在bean中实现Serializable接口
并在diskStore path="F:/ehcache"/>
文件
ehcache.xml