`
- 浏览:
3493001 次
- 性别:
- 来自:
杭州
-
使用自定义annotation接口进行aspectj动态缓存
由于系统需求需要对各个接口进行key-value缓存(以参数为key,返回的对象为value),当然对于这种情况首先考虑到的是使用aop,前段时间看过aspectj的一些介绍,借此机会正好加以应用和体会一下,aspectj是AOP最早成熟的java实现,它稍微扩展了一下java语言,增加了一些keyword等,具体的aspectj的基本语法见[ur=http://today.java.net/pub/a/today/2003/12/26/ch3AspectJSyntaxBasics.html]这里[/url],进行缓存的框架使用较成熟的ehcache.首先是ehcache的配置文件<?xmlversion="1.0"encoding="UTF-8"?><ehcache><diskStorepath="/home/workspace/gzshine/trunk/ehcache"/><cachename="DEFAULT_CACHE"maxElementsInMemory="10000"eternal="false"timeToIdleSeconds="3600"timeToLiveSeconds="3600"overflowToDisk="true"/></ehcache>这个的DEFAULT_CACHE是默认配置,最大的缓存数为10000,时间为一个小时接下来的是spring下的配置<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><!--##############aspectj4ehcache#############--><aop:aspectj-autoproxyproxy-target-class="true"/><beanid="methodCacheAspectJ"class="com.***.shine.aspectj.MethodCacheAspectJ"><propertyname="cache"><reflocal="methodCache"/></property></bean><beanid="cacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><propertyname="configLocation"><value>classpath:ehcache.xml</value></property></bean><!--定义ehCache的工厂,并设置所使用的Cachename--><beanid="methodCache"class="org.springframework.cache.ehcache.EhCacheFactoryBean"><propertyname="cacheManager"><reflocal="cacheManager"/></property><propertyname="cacheName"><value>DEFAULT_CACHE</value></property></bean><aop:aspectj-autoproxyproxy-target-class="true"/>是为aspectj在所有class下开启自动动态代理<bean id="cacheManager">指定刚刚的ehcache配置文件接下来编写一个自定义的annotationpackagecom.***.shine.cache;importjava.lang.annotation.Documented;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;@Target({ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interfaceMethodCache{intsecond()default0;<bean id ="methodCacheAspectJ">是一个aspectj进行Pointcuts和Advice的类需注入methodCachepackagecom.***.shine.aspectj;@AspectpublicclassMethodCacheAspectJ{Loglogger=LogFactory.getLog(MethodCacheAspectJ.class);privateCachecache;publicvoidsetCache(Cachecache){this.cache=cache;@Pointcut("@annotation(com.***.shine.cache.MethodCache)")publicvoidmethodCachePointcut(){@Around("methodCachePointcut()")publicObjectmethodCacheHold(ProceedingJoinPointjoinPoint)throwsThrowable{StringtargetName=joinPoint.getTarget().getClass().getName();StringmethodName=joinPoint.getSignature().getName();Object[]arguments=joinPoint.getArgs();Objectresult=null;StringcacheKey=getCacheKey(targetName,methodName,arguments);Elementelement=cache.get(cacheKey);if(element==null){try{result=joinPoint.proceed();}catch(Exceptione){logger.info(e);if(result!=null){try{element=newElement(cacheKey,(Serializable)result);ClasstargetClass=Class.forName(targetName);Method[]method=targetClass.getMethods();intsecond=0;for(Methodm:method){if(m.getName().equals(methodName)){Class[]tmpCs=m.getParameterTypes();if(tmpCs.length==arguments.length){MethodCachemethodCache=m.getAnnotation(MethodCache.class);second=methodCache.second();break;}if(second>0){//annotation没有设second值则使用ehcache.xml中自定义值element.setTimeToIdle(second);element.setTimeToLive(second);cache.put(element);}catch(Exceptione){logger.info("!!!!!!!!!"+cacheKey+"!!!!!!!!!未能执行方法缓存"+e);}returnelement.getValue();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++){if(arguments[i]instanceofDate){sb.append(".").append(DateUtil.datetoString((Date)arguments[i]));}else{sb.append(".").append(arguments[i]);}returnsb.toString();}@Pointcut("@annotation(com.netease.shine.cache.MethodCache)")对有应用com.netease.shine.cache.MethodCache进行注解的方法进行横切面拦截@Around("methodCachePointcut()")并在Advice中处理这个Pointcut,这里的的Advice使用的是Around(环绕通知)String cacheKey = getCacheKey(targetName, methodName,arguments);接下来使用类型,方法名,参数为key进入缓存处理Element element = cache.get(cacheKey);当然如果在cache队列中取得非null对象则直接返回该对象MethodCache methodCache = m.getAnnotation(MethodCache.class);second = methodCache.second();取得second的值(缓存的时间,如在@annotation中无重写只为int second() default 0)element.setTimeToIdle(second);element.setTimeToLive(second);如果非零则重新设置缓存时间@MethodCache(second=300)publicList<Sort>getSort(inttype,intparentid){System.out.println("!!!!!!!!!!!!!没缓存到");Rowrow=newRow();row.put("type",type);row.put("parentid",parentid);return(List<Sort>)gz_Template.queryForList("sort.getSort",row);----------------------------------------陈于喆Mail: chenyz@corp.netease.com
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
自定义Annotation的例子,博客地址:http://blog.csdn.net/yirentianran/article/details/52816764
NULL 博文链接:https://jeremy102.iteye.com/blog/1743919
JPA、EJB、Spring零配置等等怎么使用相信各位读者都能掌握,这里主要是说如何自定义自己的注解,自己使用自定义的注解。
sentinel-annotation-aspectj-1.4.0.jar
使用annotationProcessor插件在编译期创建Java文件 使用在idea中 可以尝试下
Spring Boot+Redis+拦截器+自定义Annotation实现接口自动幂等 在实际的开发项目中,一个对外裸露的接口往往会濒临无数次哀求,我们来说明一下幂等的概念:随意多次执行所产生的影响均与一次执行的影响相同。根据这...
一个非常简单的自定义Annotation注解demo,方便新手学习
Annotation自定义地图上的Annotation,在Annotation上加入图片。
java错误-java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Around Spring的AOP需要上述三个jar包
自定义注解annotation及用法
sentinel-annotation-aspectj-1.8.6.jar 如果下载不了,关注我,评论区联系我。
本篇文章给大家介绍了使用springboot和拦截器、redis来优雅的实现接口幂等,对于幂等在实际的开发过程中是十分重要的,因为一个接口可能会被无数的客户端调用,如何保证其不影响后台的业务处理,如何保证其只影响...
Java基础复习笔记12Java自定义注解Annotation的使用
noear::微型ORM框架(支持:java sql,xml sql,annotation sql;事务;缓存;监控;等...)可以嵌入到JVM脚本引擎(js, groovy, lua, python, ruby)及GraalVM支持的部分语言。0.2Mb(且是功能完整,方案丰富;可极...
@Target:用于描述注解的使用范围,如果自定义注解不存在@Target,则表示该注解可以使用在任何程序元素之上。接收参数ElementType,其值如下: /**接口、类、枚举、注解**/ ElementType.TYPE /**字段、枚举的常量**/...
设计思想及原理使用方法注解(Annotation)说明表达式的应用缓存删除注意事项缓存管理页面与Spring Cache的区别源码阅读已经实现基于aspectj,代码在com.jarvis.cache.aop.aspectj.AspectjAopInterceptor。想通过...
自定义Result,自定义Annotation的实例
noear微型ORM框架(支持:java sql,xml sql,annotation sql;事务;缓存;监控;等...)