java - Ehcache:找不到缓存

标签 java spring hibernate ehcache

我在我的应用程序中使用Ehcache+Spring+Hibernte,这里有一些详细信息 低于异常

Caused by: com.googlecode.ehcache.annotations.CacheNotFoundException: Unable to find cache 'com.ccc.spring.model.Validuserrole' at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getCache(CacheAttributeSourceImpl.java:223) at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.parseCacheableAnnotation(CacheAttributeSourceImpl.java:325) at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.findMethodAttribute(CacheAttributeSourceImpl.java:268) at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.computeMethodAttribute(CacheAttributeSourceImpl.java:242) at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getMethodAttribute(CacheAttributeSourceImpl.java:167) at com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl.getAdviceType(CacheAttributeSourceImpl.java:122) at com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut.matches(CacheStaticMethodMatcherPointcut.java:46) at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:225) at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262) at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88) at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:376) at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:339) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1558) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ... 21 more

这是我的 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_

最佳答案

问题是您在 Spring + EhCache 集成中使用了错误的注释。您需要使用 org.springframework.cache.annotation.Cacheable 而不是 com.googlecode.ehcache.annotations.Cacheable

查看thisthis有关如何使用 Spring 的缓存支持(随 Spring 3.1 引入)和 EhCache 作为缓存提供程序的更多信息

关于java - Ehcache:找不到缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23776736/

相关文章:

java - Hibernate 多对多映射,使用 PK 和额外列连接表

Java - 如何检测智能卡热插拔

spring - org.springframework.aop.framework.Cglib2AopProxy WARN - 无法代理方法

spring - 如何使用具有不同数据的相同测试类执行 Spring Rest Controller 测试?

java - Caused by : java.net.NoRouteToHostException: Cannot assign requested address (地址不可用)

SQL 联合到 NHibernate 标准

Java jooq 插入查询不起作用

java - android 中的 BasicAuthentication for webview 不起作用

java - @NamedNativeQuery - 如何将它绑定(bind)到存储库方法?

java - Spring Cache - 只缓存一些表