对于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包都给引入就行了!