Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 二级缓存不适用于Spring和JSF_Java_Spring_Hibernate_Jsf 2_Ehcache - Fatal编程技术网

Java 二级缓存不适用于Spring和JSF

Java 二级缓存不适用于Spring和JSF,java,spring,hibernate,jsf-2,ehcache,Java,Spring,Hibernate,Jsf 2,Ehcache,我正在使用Hibernate4+Spring4.0.3.RELEASE+JSF2.0+hibernate-ehcache4.1.0Final作为我的web应用程序,我已经在我的项目中实现了二级缓存,但这里没有代码 applicationContext.xml文件(在WEB-INF文件夹中) com.ccc.spring.model org.hibernate.dialogue.mysqldialogue 真的 真的 真的 org.hibernate.cache.EhCacheProvider

我正在使用Hibernate4+Spring4.0.3.RELEASE+JSF2.0+hibernate-ehcache4.1.0Final作为我的web应用程序,我已经在我的项目中实现了二级缓存,但这里没有代码

applicationContext.xml文件(在WEB-INF文件夹中)


com.ccc.spring.model
org.hibernate.dialogue.mysqldialogue
真的
真的
真的
org.hibernate.cache.EhCacheProvider
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
真的
ehcache.xml文件(在WEB-INF文件夹中)


在DAO文件中,我使用以下代码

 @Cacheable( value = "ValidUserRole" )
    public List<Validuserrole> getRoleList() {
        List list = null;
        try{
          list = getSessionFactory().getCurrentSession()
                .createQuery("from Validuserrole ").list();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }
@Cacheable(value=“ValidUserRole”)
公共列表getRoleList(){
List=null;
试一试{
list=getSessionFactory().getCurrentSession()
.createQuery(“来自Validuserrole”).list();
}
捕获(例外e){
e、 printStackTrace();
}
退货清单;
}

但是,当我调试代码时,每次都从数据库获取数据,而不是从二级缓存获取数据时,你混合了两种不同的东西,它们是两种不同的野兽。你正试图把两者混合在一起。更糟糕的是,您试图使用Spring自己的缓存支持的前身

缓存方法结果

首先,移除spring ehcache,然后切换到。这将使
@Cacheable
注释正常工作。如果这是您想要的,那么就不用二级缓存了

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"      
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    http://www.springframework.org/schema/cache
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:sec="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                 http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
                 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<cache:annotation-driven />

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <constructor-arg ref="ehcacheManager" />
</bean>

<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="/WEB-INF/ehcache.xml" />
</bean>
接下来将执行查询,但结果对象(可缓存时)将保存在内存中。然后,下次需要相同的实体实例时,将返回缓存中的实例

查询缓存

如果要启用查询缓存,则必须使其处于非缓存状态

@Entity
@Cacheable // Note this is from hibernate NOT spring!!!
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Validuserrole { ... }
list = getSessionFactory().getCurrentSession()
            .createQuery("from Validuserrole ")
            .setCacheable(true)
            .list();

为什么不使用Springs自己的缓存支持?另外,您需要的不是二级缓存,而是结果缓存。二级缓存是一种ORM提供程序(在本例中为hibernate)。因此,如果没有更好的选择,那么这不是使用二级缓存的方法?关键是您可以混合使用查询缓存、二级缓存和spring(或spring ehcache)缓存支持。你的意思是我不需要你添加和删除ehcache标记吗?正确,这已经在你的
ehcache.xml
中了,接下来的
东西(spring-ehcache项目)从2011年起就已经死了(在spring引入了自己的缓存抽象之后)。同样,这对二级缓存也没有任何作用,因为这两个东西是不相关的。所以仍然缺少一些东西,我无法将类型为“net.sf.ehcache.CacheManager”的属性值转换为属性“CacheManager”所需的类型“org.springframework.cache.CacheManager”;嵌套异常为java.lang.IllegalStateException:无法将[net.sf.ehcache.CacheManager]类型的值转换为属性“CacheManager”所需的[org.springframework.cache.CacheManager]类型:没有匹配的编辑器或转换策略foundMy bad,缺少一个bean。请参阅修改后的答案。这基本上已经得到了回答。您需要正确配置缓存。i、 e.缓存的有效期有多长等。这些都是可配置的。我强烈建议您阅读ehcache的工作原理以及如何配置它。
@Entity
@Cacheable // Note this is from hibernate NOT spring!!!
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Validuserrole { ... }
list = getSessionFactory().getCurrentSession()
            .createQuery("from Validuserrole ")
            .setCacheable(true)
            .list();