首先需要的maven依赖为:
-
-
- com.googlecode.ehcache-spring-annotations
- ehcache-spring-annotations
- ${ehcache-spring.version}
- jar
- compile
-
-
- net.sf.ehcache
- ehcache-core
- ${ehcache-core.version}
-
spring的maven依赖就不在展示出来。
ehcache需要一个配置文件,为ehcache.xml,内容为:
-
-
-
-
-
-
-
-
- classpath:/ehcache.xml
-
-
-
-
-
-
-
-
-
- DEFAULT_CACHE
-
-
如此配置,基本spring整合ehcache就完成了,但还是要单独测试一下,编写一个测试程序:
- package cn.com.ecache;
-
- import cn.com.container.ServiceProvinder;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.Element;
- import org.junit.Test;
-
- /**
- * Created by Administrator on 2016/1/24.
- */
- public class TestEcache {
-
- @Test
- public void Test() {
- Cache cache = (Cache) ServiceProvinder.getService("ehCache");
- Element lgElement = new Element("loginName", "xiaxuan");
- Element pwElement = new Element("password", "xiaxuan");
- cache.put(lgElement);
- cache.put(pwElement);
- System.out.println(cache.get("loginName"));
- }
- }
测试结果为:

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