2011-07-05 95 views
2

我有一個應用程序,我使用spring 3.0.2和ibatis。現在,我需要將ehcache與我的代碼結合起來。我試過this link,但無法讓它工作。我寧願有人給我所需的jar包的詳細信息,要完成的xml配置和需要的代碼更改。將ehcache與spring 3.0集成

回答

1

要在應用程序中實現這一點,請按照下列步驟:

第1步:

添加的罐子到您的應用程序上Ehcache Annotations for Spring project site所列。

第2步:

註釋添加到您想緩存的方法。讓我們假設你使用的是狗getDog(字符串名稱)方法從上面:

@Cacheable(name="getDog") 
Dog getDog(String name) 
{ 
    .... 
} 

第3步:

配置Spring。你必須將以下內容添加到豆聲明部分Spring配置文件:

<ehcache:annotation-driven cache-manager="ehCacheManager" /> 

請參考Ehcache site完整的詳細信息。

0

爲了整合了Ehcache只要按照下面的步驟

1 - 添加依賴關係在POM XML文件

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

2 - 創建一個XML文件,稱爲彈簧cache.xml把它的資源文件夾

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="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"> 

    <cache:annotation-driven/> 

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
     <property name="cacheManager" ref="ehcache" /> 
    </bean> 

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
     <property name="configLocation" value="classpath:ehcache.xml" /> 
    </bean> 

</beans> 

3 - 你可以看到,我們正在使用的ehcache.xml中引用這樣創建文件,並把它的資源文件夾

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="true" 
    monitoring="autodetect" dynamicConfig="true"> 
    <cache name="users" maxEntriesLocalHeap="5000" 
     maxEntriesLocalDisk="1000" eternal="false" diskSpoolBufferSizeMB="20" 
     timeToIdleSeconds="200" timeToLiveSeconds="500" 
     memoryStoreEvictionPolicy="LFU" transactionalMode="off"> 
     <persistence strategy="localTempSwap" /> 
    </cache> 
</ehcache> 

因此可以看到創建「用戶」的高速緩存,這樣可以使用任何地方的用戶列表是從數據庫中查詢

4 - 使用它像下面的代碼

@Cacheable(value="users") 
public List<User> userList() { 
    return userDao.findAll(); 
} 

所以這是它同任何需要

仍然有一些疑問或者困惑,您就可以實現緩存看到現場演示

Integrate EhCache in Spring MVC