BeanFactory.servletContext = servletContext;
}
}
为了记录浏览器端的访问记录可以写一个log切面,最后是切面的申明配置。
以上两个配置文件都是作为spring bean管理的。
三、缓存切面类:
[java]
@Aspect
public class CacheAspect implements Ordered {
private int orderValue = 2;
private Cache cacheManager;
private Logger log = Logger.getLogger(CacheAspect.class);
@Pointcut("@annotation(com.cr.common.cache.core.NeedCached)")
public void needCached() {
}
@Around("needCached() && args(filter,..)")
public Object aroundMethod(ProceedingJoinPoint pjp, QueryFilter filter)
throws Throwable {
if (filter.getValue() == null) {
return null;
} www.2cto.com
// 判断是否开启缓存
boolean cacheEnabled = CommonUtil.parseBoolean(ContextConfig.getProperty("cache.enabled"), false);
if (cacheEnabled) {
String md5key = MD5Util.getMD5(filter.getValue().toString());
Object value = cacheManager.getCacheInfo(md5key);
boolean flag = false; if (null != value) JSONObject json = new JSONObject(value.toString());
return json;
} else if ("null".equals(value)) {
return null;
if(null!=value){//此处省略若干业务码 cacheManager.setCacheInfo(md5key, value.toString());
}
return value;
}
} else {// 未开启缓存,执行数据库查询
return pjp.proceed();
}
}
public void setOrderValue(int orderValue) {
this.orderValue = orderValue;
}
public int getOrder() {
return orderValue;
}
public int getOrderValue() {
return orderValue;
}
public Cache getCacheManager() {
return cacheManager;
}
public void setCacheManager(Cache cacheManager) {
this.cacheManager = cacheManager;
}
}
needCached是自定义注解,用于需要执行缓存切面的方法上,aroundMethod环绕在含有此注解,及方法参数含有filter的方法上面。由于本来的key超过了250,所以md5后作为key,value是jsonObect。
查询时,若缓存有,直接返回,没有则执行数据库查询,再设置缓存。