当前位置: 首页 > 知识库问答 >
问题:

Spring3.1 和 hibernate4 eh-cache

湛功
2023-03-14

我的web应用程序使用的是Spring3.1和hibernate4。在这里,我试图为eh缓存,但得到一些错误,这是我的配置,我已经使用:-

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 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 />
    <bean id="defaultEhCacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:config-location="/WEB-INF/ehcache.xml" p:shared="false"></bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"></property>
    </bean>
    <cache:annotation-driven cache-manager="cacheManager" />
    <bean id="cacheManager">
        <property name="cacheManager" ref="ehcache" />
    </bean>
    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="/WEB-INF/ehcache.xml" />
    </bean> 

ehcache.xml

<cache name="sampleCache1" 
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600">
</cache>

这是依赖性:—

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.5.2</version>
</dependency>

我得到以下错误:--

严重:在org.html" target="_blank">springframework.beans.factory中上下文初始化失败。BeanCreationException:创建名为“org.springframework.cache.interceptor”的bean时出错。CacheInterceptor#0:设置bean属性“cacheManager”时无法解析对bean“CacheMnager”的引用;嵌套的异常是org.springframework.beans.factory。BeanCreationException:创建名为“cacheManager”的bean时出错,该bean在ServletContext资源[/WEB-INF/dispatcher servlet.xml]中定义:设置bean属性“cacheManager”时无法解析对bean“ehcache”的引用;嵌套的异常是org.springframework.beans.factory。NoSuchBeanDefinitionException:在上未定义名为“ehcache”的bean

请尽快提出任何解决方案。

预先感谢

共有1个答案

袁晟
2023-03-14

这种配置对我来说很好:

META-INF/Spring/sandbox-cache-context.xml

<?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:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

    <bean id="ehCacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:configLocation="classpath:META-INF/ehcache.xml"
        p:shared="false" />

    <bean id="cacheManager"
        class="org.springframework.cache.ehcache.EhCacheCacheManager"
        p:cacheManager-ref="ehCacheManager" />

    <cache:annotation-driven cache-manager="cacheManager" />

</beans>

META-INF/ehcache.xml

<ehcache>
    <diskStore path="java.io.tmpdir" />

    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        overflowToDisk="true"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600" />

    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        maxElementsOnDisk="10000000"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
</ehcache>

CacheContextTest.java

import static org.fest.assertions.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:META-INF/spring/sandbox-cache-context.xml")
public class CacheContextTest {

    @Autowired
    CacheManager cacheManager;

    @Test
    public void shouldStartContext() throws Exception {
        assertThat(cacheManager.getCache("sampleCache1")).isNotNull();
        assertThat(cacheManager.getCache("nonExistentCache")).isNull();
    }
}
 类似资料:
  • 我在Spring3.1中使用@Cacheable。我对Cacheable中的值和键映射参数有点混淆。以下是我正在做的: 这里发生的情况是,第二个方法依赖于第一个方法的选定值,但问题是假设当我传递zoneMastNo=1和areaMastNo=1时,第二个方法返回第一个方法结果。事实上,我有很多服务,因此,我希望使用公共值来缓存特定的用例。现在我的问题是: 我如何解决这个问题 对每个服务都使用cac

  • 我正在尝试实现Spring3.1缓存,正如这里和这里所解释的,但它似乎不起作用:我的方法每次都会运行,即使它被标记为@cacheable。我做错了什么? 我已经将它移动到一个带有自己配置文件的junit测试用例中,以将它与应用程序的其余部分隔离开来,但问题仍然存在。以下是相关文件: Spring-test-servlet.xml ehcache.xml 我的测试。JAVA 相关Pom条目:(spr

  • 我一直在尝试使用Spring3.1的bean定义概要文件和嵌套bean。我希望可以根据活动配置文件定义不同的bean。请考虑以下非常简化的示例,使我的Spring上下文包含如下内容 我得到以下错误: 线程“main”org.springframework.beans.factory.BeanCreationException中出现异常:创建类路径资源[applicationcontext.xml]

  • 在 Application(应用)面板检查和管理Storage(存储)、 Databases(数据库)和Caches(缓存)。 TL;DR 查看和编辑本地和会话存储。 检查和修改 IndexedDB 数据库。 在Web SQL数据库上执行语句。 查看 Application 和 Service Worker 缓存。 单击按钮,清除所有存储,数据库,缓存和service workers。 Local

  • 我有一个用并尝试使用注释。该方法还用注释。将忽略注释。是否可以在Mapstruct方法上使用?

  • 问题内容: 关于它们有很多传说。我想知道真相。以下两个示例之间有什么区别? 问题答案: 不确定从何处获得传说,但: 提交按钮 与: IE6将在标记之间提交此按钮的所有文本,其他浏览器将仅提交值。使用可使您在按钮的设计上享有更大的布局自由度。从各种意图和目的看,它乍一看似乎很棒,但是各种浏览器怪癖使它有时很难使用。 在您的示例中,IE6将发送到服务器,而其他大多数浏览器将不发送任何内容。要使其跨浏览