首先需要的maven依赖为:

[html] view
plain
 copy

  1. <!--ehcache-->
  2. <dependency>
  3. <groupId>com.googlecode.ehcache-spring-annotations</groupId>
  4. <artifactId>ehcache-spring-annotations</artifactId>
  5. <version>${ehcache-spring.version}</version>
  6. <type>jar</type>
  7. <scope>compile</scope>
  8. </dependency>
  9. <dependency>
  10. <groupId>net.sf.ehcache</groupId>
  11. <artifactId>ehcache-core</artifactId>
  12. <version>${ehcache-core.version}</version>
  13. </dependency>

spring的maven依赖就不在展示出来。

ehcache需要一个配置文件,为ehcache.xml,内容为:

[html] view
plain
 copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
  4. <diskStore path="java.io.tmpdir" /> <!-- 缓存存放目录(此目录为放入系统默认缓存目录),也可以是”D:/cache“ java.io.tmpdir -->
  5. <defaultCache
  6. maxElementsInMemory="10000"
  7. eternal="false"
  8. timeToIdleSeconds="120"
  9. timeToLiveSeconds="120"
  10. overflowToDisk="true"
  11. maxElementsOnDisk="10000000"
  12. diskPersistent="false"
  13. diskExpiryThreadIntervalSeconds="120"
  14. memoryStoreEvictionPolicy="LRU"
  15. />
  16. <!--
  17. name:Cache的唯一标识
  18. maxElementsInMemory:内存中最大缓存对象数
  19. maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
  20. eternal:Element是否永久有效,一但设置了,timeout将不起作用
  21. overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
  22. timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大
  23. timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大
  24. diskPersistent:是否缓存虚拟机重启期数据
  25. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒
  26. diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
  27. memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)
  28. -->
  29. </ehcache>

在spring的applicationContext.xml的配置文件的编写为:

[html] view
plain
 copy

  1. <!-- 引用ehCache的配置 -->
  2. <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  3. <property name="configLocation">
  4. <value>classpath:/ehcache.xml</value>
  5. </property>
  6. </bean>
  7. <!-- 定义ehCache的工厂,并设置所使用的Cache name -->
  8. <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  9. <property name="cacheManager">
  10. <ref local="defaultCacheManager"/>
  11. </property>
  12. <property name="cacheName">
  13. <value>DEFAULT_CACHE</value>
  14. </property>
  15. </bean>

如此配置,基本spring整合ehcache就完成了,但还是要单独测试一下,编写一个测试程序:

[java] view
plain
 copy

  1. package cn.com.ecache;
  2. import cn.com.container.ServiceProvinder;
  3. import net.sf.ehcache.Cache;
  4. import net.sf.ehcache.Element;
  5. import org.junit.Test;
  6. /**
  7. * Created by Administrator on 2016/1/24.
  8. */
  9. public class TestEcache {
  10. @Test
  11. public void Test() {
  12. Cache cache = (Cache) ServiceProvinder.getService("ehCache");
  13. Element lgElement = new Element("loginName", "xiaxuan");
  14. Element pwElement = new Element("password", "xiaxuan");
  15. cache.put(lgElement);
  16. cache.put(pwElement);
  17. System.out.println(cache.get("loginName"));
  18. }
  19. }

测试结果为:

既可以存放数据到缓存中,又可以从缓存中拿到数据,spring和ehcache的整合基本成功。

如上,就是spring和ehcache整合。

Ehcache学习总结(2)--Ehcache整合spring配置的更多相关文章

  1. Ehcache学习总结(3)--Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式 ...

  2. Ehcache整合spring配置,配置springMVC缓存

    为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和spring整合配置. 1.   需要的jar包 slf4j-api-1.6.1.jar ehcache-c ...

  3. Ehcache整合spring配置

    为了提高系统的运行效率,引入缓存机制,减少数据库访问和磁盘IO.下面说明一下ehcache和spring整合配置. 1.   需要的jar包 slf4j-api-1.6.1.jar ehcache-c ...

  4. 终极版:Mybatis整合Spring配置

    第一部分:配置 Spring 框架 配置 SpringMVC 的步骤: 配置流程图: SpringMVC 配置 1. 导入包(那些包,基本包 5 个,1 个日志依赖包,2 个 webmvc 支持包) ...

  5. Mybatis学习笔记(八) —— Mybatis整合spring

    一.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...

  6. Ehcache学习总结(1)--Ehcache入门介绍

    Ehcache是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多,如果你有这方面的 ...

  7. ssm整合——Spring配置(2)

    配置Spring 1. 环境准备 使用之前搭建Mabatis的环境 1.1 新建目录 新建spring的service业务逻辑包 在resources目录下新建spring的配置文件:applicat ...

  8. Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...

  9. (转)Ehcache 整合Spring 使用页面、对象缓存

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

随机推荐

  1. shell编程笔记1

    参考文章:1 http://blog.csdn.net/wuwenxiang91322/article/details/9259877   通过chmod改变文件权限 补充知识: 1Linux文件的三 ...

  2. SQL中一次插入多条数据

    SQL中insert一次可以插入一条数据,我们有三种方法可以一次性插入多条数据. 1. 语法:select 字段列表 into 新表 from 源表 注意事项:此种方法新表是系统自动创建,语句执行前不 ...

  3. class-metaclass-Class vs. type

    In some languages, classes are only a compile-time feature (new classes cannot be declared at runtim ...

  4. 优动漫 PAINT 导航窗口面板

    导航窗口面板用于管理画布的视图.本节将介绍如何在其中缩放.旋转.翻转画布. 如何使用导航窗口面板 使用[导航窗口]面板便于把握整个画布,以下将为您详细介绍. 什么是[导航窗口]面板 [导航窗口]面板用 ...

  5. SAI / PS绘画一个卡通女孩详解

    本教程介绍使用SAI / PS绘画一个卡通女孩的教程 ,教程很详细,动起你的小手一起来试试吧! 软件下载:http://www.dongmansoft.com/xiazai.html 想要Get到更多 ...

  6. matplotlib bar函数重新封装

    参考: https://blog.csdn.net/jenyzhang/article/details/52047557 https://blog.csdn.net/liangzuojiayi/art ...

  7. CentOS7 部署SVN服务器

    服务器端:svnserver 安装主要步骤 yum install subversion rpm -ql subversion mkdir /application/svndata mkdir /ap ...

  8. Springboot+swagger2的接口文档开发

    一.创建一个SpringBoot项目 1. 2. 3. 4. 把web里的web选中,SQL里选择自己需要的,点击next 二.创建各项所需的controller,configure等 1. 项目布局 ...

  9. (QT)在命令行编译ui文件和程序

    1.新建helloworld_2文件夹,将helloworld里的main.cpp和hellodialog.cpp两个文件复制过来. 2.打开控制台.此时不能用cmd,否则不能出最后的结果(lz在运行 ...

  10. dos2unix和unix2dos命令使用【转】

    dos2unix, unix2dos 用来实现 DOS <=> UNIX text file 转换 aptitude install sysutils 行末: DOS 格式 0d 0a U ...