首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

spring配备ehcache

2013-01-23 
spring配置ehcache1.引入需要的jar2.配置ehcache,ehcache.xml内容如下:public class EhcacheSpringTest {p

spring配置ehcache

1.引入需要的jar

2.配置ehcache,ehcache.xml内容如下:

public class EhcacheSpringTest {private CacheManager cacheManager;private Cache cache;@Beforepublic void init() {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");EhCacheCacheManager ehCacheManager = (EhCacheCacheManager) ctx.getBean("cacheManager");cacheManager = ehCacheManager.getCacheManager();cache = cacheManager.getCache("userCache");}@Afterpublic void destory() {cacheManager.shutdown();}@Testpublic void testEhcacheString() {String key = "hello";String value = "world";cache.put(new Element(key, value));Element element = cache.get(key);Object obj = element.getObjectValue();Assert.assertEquals(value, obj);System.out.println(obj);}@Testpublic void testEhcacheObj() {String key = "user";User value = new User("zhangsan", "lisi");cache.put(new Element(key, value));Element element = cache.get(key);Object obj = element.getObjectValue();Assert.assertEquals(value, obj);System.out.println(obj);Object objSer = element.getValue();Assert.assertEquals(value, objSer);System.out.println(objSer);}}


热点排行