问题描述
如题配置文件XMLcode<!--缓存管理--><beanid="defaultCacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><propertyname="configLocation"><value>ehcache.xml</value></property></bean><!--定义ehCache的工厂,并设置所使用的Cachename--><beanid="ehCache"class="org.springframework.cache.ehcache.EhCacheFactoryBean"><propertyname="cacheManager"><reflocal="defaultCacheManager"/></property><propertyname="cacheName"><value>DEFAULT_CACHE</value></property></bean><beanid="methodCacheInterceptor"class="com.longtuo.server.util.MethodCacheInterceptor"><propertyname="cache"><reflocal="ehCache"/></property></bean><beanid="methodCacheAfterAdvice"class="com.longtuo.server.util.MethodCacheAfterAdvice"><propertyname="cache"><reflocal="ehCache"/></property></bean><beanid="methodCachePointCut"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><propertyname="advice"><reflocal="methodCacheInterceptor"/></property><propertyname="patterns"><list><value>.*query.*</value><value>.*get.*</value></list></property></bean><beanid="methodCachePointCutAdvice"class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><propertyname="advice"><reflocal="methodCacheAfterAdvice"/></property><propertyname="patterns"><list><value>.*create.*</value><value>.*update.*</value><value>.*delete.*</value></list></property></bean><beanid="couponServices"class="org.springframework.aop.framework.ProxyFactoryBean"><propertyname="target"><reflocal="couponDao"/></property><propertyname="interceptorNames"><list><value>methodCachePointCut</value><value>methodCachePointCutAdvice</value></list></property></bean>MethodCacheInterceptor类文件Javacodepackagecom.longtuo.server.util;importjava.io.Serializable;importnet.sf.ehcache.Cache;importnet.sf.ehcache.Element;importorg.aopalliance.intercept.MethodInterceptor;importorg.aopalliance.intercept.MethodInvocation;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.springframework.beans.factory.InitializingBean;importorg.springframework.util.Assert;publicclassMethodCacheInterceptorimplementsMethodInterceptor,InitializingBean{privatestaticfinalLoglogger=LogFactory.getLog(MethodCacheInterceptor.class);privateCachecache;publicvoidsetCache(Cachecache){this.cache=cache;}publicMethodCacheInterceptor(){super();}/***拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值,否则,返回数据库查询结果,并将查询结果放入cache*/publicObjectinvoke(MethodInvocationinvocation)throwsThrowable{StringtargetName=invocation.getThis().getClass().getName();StringmethodName=invocation.getMethod().getName();Object[]arguments=invocation.getArguments();Objectresult;logger.debug("Findobjectfromcacheis"+cache.getName());StringcacheKey=getCacheKey(targetName,methodName,arguments);Elementelement=cache.get(cacheKey);if(element==null){logger.debug("Holdupmethod,Getmethodresultandcreatecache........!");result=invocation.proceed();element=newElement(cacheKey,(Serializable)result);cache.put(element);}returnelement.getValue();}/***获得cachekey的方法,cachekey是Cache中一个Element的唯一标识cachekey包括*包名+类名+方法名,如com.co.cache.service.UserServiceImpl.getAllUser*/privateStringgetCacheKey(StringtargetName,StringmethodName,Object[]arguments){StringBuffersb=newStringBuffer();sb.append(targetName).append(".").append(methodName);if((arguments!=null)&&(arguments.length!=0)){for(inti=0;i<arguments.length;i++){sb.append(".").append(arguments[i]);}}returnsb.toString();}/***implementInitializingBean,检查cache是否为空*/publicvoidafterPropertiesSet()throwsException{Assert.notNull(cache,"Needacache.PleaseusesetCache(Cache)createit.");}}为什么不能对dao的query方法缓存