[b]EHCache配置文件ehcache.xml[/b]
[b]Spring相关配置[/b]
以下为核心切面Bean代码
[b]以下为ehcache的jar以及aspect支持所需jar[/b]
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="7200"
memoryStoreEvictionPolicy="LFU">
</defaultCache>
<cache name="StuCache"
maxElementsInMemory="200"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU">
</cache>
</ehcache>
[b]Spring相关配置[/b]
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
<!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy/>
<!-- 定义切面类 -->
<bean class="zl.AspectDemo"/>
<!-- 引用ehCache的配置和管理类 -->
<bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<!-- 定义ehCacheBean,并设置所使用的缓存名字 -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager"/>
</property>
<property name="cacheName">
<value>StuCache</value>
</property>
</bean>
</beans>
以下为核心切面Bean代码
package zl.aop;
import javax.annotation.Resource;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
//注解表示这个为切面Bean
@Aspect
public class AspectDemo {
private static final Log logger = LogFactory.getLog(StudentAction.class);
private Cache cache;
//注入缓存容器
@Resource(name="ehCache")
public void setCache(Cache cache) {
this.cache = cache;
}
//定义一个切入点:业务层中所有以Impl结尾的类的get方法 参数不限 返回值不限
@Pointcut("execution(* zl.service.impl.*Impl.get*(..))")
public void demoPointcut(){}
//环绕增强 切入段为demoPointcut
@Around("demoPointcut()")
public Object demo(ProceedingJoinPoint jp) throws Throwable{
//得到 目标对象名
String targetName = jp.getTarget().toString();
//得到 目标方法名
String methodName = jp.getSignature().getName();
//得到 目标方法参数个数
int argLength = jp.getArgs().length;
//拼接一个由以上3个组成的可以作为唯一键的字符串
String cacheKey = getCacheKey(targetName, methodName, argLength);
//在缓存中用key查找
Element element = cache.get(cacheKey);
if(element == null){
//如果没有
logger.debug("缓存中没有,执行数据访问读取数据...");
// proceed(..)为执行目标方法 jp.getArgs()为方法的参数
Object result = jp.proceed(jp.getArgs());
//创建一个新的缓存对象
element = new Element(cacheKey,result);
//放入缓存容器
cache.put(element);
}
//返回 缓存对象所封装的结果集
return element.getObjectValue();
}
//拼接一个key
private String getCacheKey(String targetName,String methodName,int argLength){
StringBuffer sb = new StringBuffer();
sb.append(targetName).append("_")
.append(methodName).append("_")
.append(argLength);
return sb.toString();
}
}
[b]以下为ehcache的jar以及aspect支持所需jar[/b]