Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现。它支持注解方式使用缓存,非常方便。

spring本身内置了对Cache的支持,之前记录的是基于Java API的ConcurrentMap的CacheManager配置,现使用ehcache实现。

1、引入相关依赖

    <!-- 引入ehcache缓存 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache-version}</version>
</dependency>

2、声明对cache的支持

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"> <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven/> </beans>

2、配置CacheManager

先使用Spring提供的EhCacheCacheManager来生成一个Spring的CacheManager,让其接收一个Ehcache的CacheManager,因为真正用来存入缓存数据的还是Ehcache。

Ehcache的CacheManager是通过Spring提供的EhCacheManagerFactoryBean来生成的,它可以通过指定ehcache的配置文件位置来生成一个Ehcache的CacheManager.

若未指定则将按照Ehcache的默认规则取classpath根路径下的ehcache.xml文件,若该文件也不存在,则获取Ehcache对应jar包中的ehcache-failsafe.xml文件作为配置文件。

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

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"> <!-- 默认缓存 -->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"/> <!-- 用户详情缓存 -->
<cache name="userDetailCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/> </ehcache>

还有,以上说的是Spring内置的对Cache的支持,也可以通过Spring自己单独的使用Ehcache的CacheManager或Ehcache对象。

在配置文件中配置EhCacheManagerFactoryBean和EhCacheFactoryBean

1、EhCacheManagerFactoryBean

EhCacheManagerFactoryBean是Spring内置的一个可以产生Ehcache的CacheManager对象的FactoryBean。

可以通过属性configLocation指定用于创建CacheManager的Ehcache配置文件的路径,通常是ehcache.xml文件的路径。

如果没有指定configLocation,则将使用默认位置的配置文件创建CacheManager(即如果在classpath根路径下存在ehcache.xml文件,则直接使用该文件作为Ehcache的配置文件,否则将使用ehcache-xxx.jar中的ehcache-failsafe.xml文件作为配置文件来创建Ehcache的CacheManager)。

2、EhCacheFactoryBean

EhCacheFactoryBean是用来产生Ehcache的Ehcache对象的FactoryBean。

cacheManager属性,其可以指定将用来获取或创建Ehcache的CacheManager对象,若未指定则将通过CacheManager.create()获取或创建默认的CacheManager。

cacheName属性,其表示当前EhCacheFactoryBean对应的是CacheManager中的哪一个Ehcache对象,若未指定默认使用beanName作为cacheName。若CacheManager中不存在对应cacheName的Ehcache对象,则将使用CacheManager创建一个名为cacheName的Cache对象。

    <!-- 定义CacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<!-- 指定配置文件的位置 -->
<property name="configLocation" value="classpath:config/ehcache.xml"/>
<!-- 指定新建的CacheManager的名称 -->
<property name="cacheManagerName" value="cacheManagerName"/>
</bean> <!-- 定义一个Ehcache -->
<bean id="userCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheName" value="user"/>
<property name="cacheManager" ref="cacheManager"/>
</bean>

最新文章

  1. jQuery-1.9.1源码分析系列(十二) 筛选操作
  2. 新手使用django-pagination分页
  3. MFC MDI 主框架和标签页数据互操作
  4. Android自学笔记:环境配置
  5. RAC和ASM环境下修改控制文件control file
  6. 非关系型数据库SequoiaDB虚拟机下应用再探
  7. 2016.08.06计算几何总结测试day1
  8. Effective C++ 笔记二 构造/析构/赋值运算
  9. (转载)mysql 存在该记录则更新,不存在则插入记录的sql
  10. 什么是web标准
  11. TCP的阻塞和重传
  12. 序列化与反序列化中serialVersionUID的作用(通俗易懂)
  13. ssh远程访问失败 Centos7
  14. Android Studio遇到Failed to sync Gradle project错误时的解决办法
  15. SDWebImageRefreshCached
  16. WinCE平台的程序编译到Win32平台下运行
  17. response.setContentType()的String参数及对应类型
  18. python中的一个现象,db.commit和db.commit()
  19. 由ngx.say和ngx.print差异引发的血案
  20. jitter

热门文章

  1. h3c 瘦ap无法上线解决办法(WA4320i-ACN)
  2. 12 Spring Data JPA:springDataJpa的运行原理以及基本操作(上)
  3. LaTeX Windows配置
  4. 面向对象 part7 class
  5. AFN Post请求,报错400(code:-1011)
  6. NWERC 2015
  7. 纯手撸web框架
  8. 【Java杂货铺】JVM#虚拟机加载机制
  9. 2020 CCPC Wannafly Winter Camp Day2-K-破忒头的匿名信
  10. 自定义字段从BOM带入生产用料清单