對于spring cache的配置和使用網上說了很多了,不再重複了,現在描述一下所使用的jar包
一 使用spring本身的cache
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
二 使用ehcache
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.1</version>
</dependency>
特别說明:在使用ehcahe實作緩存的時候,不加入ehcache-2.8.1.jar的時候,在spring加入配置檔案:
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="ehcache.xml"/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehcache"/>
時, 在為cacheManager指定cacheManager引用時, IDE一直提示ehcache應該為net.sf.ehcache.CacheManager類型的,看了一下源碼,
在類org.springframework.cache.ehcache.EhCacheCacheManager類中的cacheManager類型确實為net.sf.ehcache.CacheManager,
public class EhCacheCacheManager extends org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager {
private net.sf.ehcache.CacheManager cacheManager;
而引用ehcache為org.springframework.cache.ehcache.EhCacheManagerFactoryBean類型的,
進入類org.springframework.cache.ehcache.EhCacheManagerFactoryBean的源碼可以看到:
public class EhCacheManagerFactoryBean implements org.springframework.beans.factory.FactoryBean<net.sf.ehcache.CacheManager>,
org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean {
類org.springframework.cache.ehcache.EhCacheManagerFactoryBean實作接口org.springframework.beans.factory.FactoryBean<net.sf.ehcache.CacheManager>
并且傳入了net.sf.ehcache.CacheManager參數類型
而且類org.springframework.cache.ehcache.EhCacheManagerFactoryBean 有方法:
public net.sf.ehcache.CacheManager getObject() { /* compiled code */ }
是以引入jar包ehcache-2.8.1.jar之後,spring的配置檔案就不會再報錯了!
說的非常亂,不過記住把上面三個jar包都給引入就行了!