读书人

应用Spring AOP设置Cache

发布时间: 2012-08-31 12:55:03 作者: rapoo

使用Spring AOP设置Cache
一,选择缓存EHCache
毫无疑问ehcache.1.2.4.jar,这个发行包必须加入项目。

二,ehcache.xml 加入classpath


其中Cache各各属性表示意义,可望文而知。

三,在applicationContext.xml加入缓存管理组件。


四,缓存使用演示
@Aspect@Componentpublic class CacheInterceptor extends BaseInterceptor{@Autowired@Qualifier("ehcacheManager")private CacheManager cm;//@Autowired//@Qualifier("cache")//private Cache cache;@Override@Around("execution(* cn.com.legendapl.spring.dao.impl.*.findAllUsers(..))")public Object around(ProceedingJoinPoint jp) throws Throwable {Cache cache = cm.getCache("legendapl");String key = "";Object[] args = jp.getArgs();for (Object o : args) {key += o.toString();}Object result = null;Element element = cache.get(key);if (element != null) {result = element.getValue();} else {result = jp.proceed();cache.put(new Element(key, result));}return result;}}

读书人网 >软件架构设计

热点推荐