您当前的位置: 首页 >  spring

科技D人生

暂无认证

  • 0浏览

    0关注

    1550博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Ehcache学习总结(2)--Ehcache整合spring配置

科技D人生 发布时间:2016-07-06 10:04:17 ,浏览量:0

  首先需要的maven依赖为:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.   
  2.         
  3.         com.googlecode.ehcache-spring-annotations  
  4.         ehcache-spring-annotations  
  5.         ${ehcache-spring.version}  
  6.         jar  
  7.         compile  
  8.         
  9.         
  10.         net.sf.ehcache  
  11.         ehcache-core  
  12.         ${ehcache-core.version}  
  13.         
spring的maven依赖就不在展示出来。

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

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.   
  2.     
  3.        
  4.        
  5.        
  6.         
  7.       
  8.           
  9.             classpath:/ehcache.xml  
  10.           
  11.       
  12.   
  13.       
  14.       
  15.           
  16.               
  17.           
  18.           
  19.             DEFAULT_CACHE  
  20.           
  21.       
如此配置,基本spring整合ehcache就完成了,但还是要单独测试一下,编写一个测试程序:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package cn.com.ecache;  
  2.   
  3. import cn.com.container.ServiceProvinder;  
  4. import net.sf.ehcache.Cache;  
  5. import net.sf.ehcache.Element;  
  6. import org.junit.Test;  
  7.   
  8. /** 
  9.  * Created by Administrator on 2016/1/24. 
  10.  */  
  11. public class TestEcache {  
  12.   
  13.     @Test  
  14.     public void Test() {  
  15.         Cache cache = (Cache) ServiceProvinder.getService("ehCache");  
  16.         Element lgElement = new Element("loginName", "xiaxuan");  
  17.         Element pwElement = new Element("password", "xiaxuan");  
  18.         cache.put(lgElement);  
  19.         cache.put(pwElement);  
  20.         System.out.println(cache.get("loginName"));  
  21.     }  
  22. }  

测试结果为:

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

       如上,就是spring和ehcache整合。

关注
打赏
1662604032
查看更多评论
立即登录/注册

微信扫码登录

0.0475s