




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
spring4中文技术手册在静态代理和动态代理中提到了面向方面编程,主要就是基于动态代理。单独抽象出非业务的功能,服务于某些业务方法。Spring提供了四种很实用的Advice,分别为:Before Advice, After Returning Advice, Around Advice, After throwing Advice。都是方法级别的,就是在某个方法执行前后插入一些非业务的操作,如打日志或者判断权限等。对于这四种advice的实现,spring都提供了三种方法,分别为基于接口、基于xml和基于annotation(注释)。Before Advice会在目标对象的方法执行之前被调用;After Advice会在目标方法执行之后被调用;Around Advice则可以在目标方法执行前后同时加上相关服务;Throw Advice是在异常发生后执行某些操作。1.基于接口的Advice这个就需要自定义的Aspect实现Spring接口。BeforeAdvice需要实现org.springframework.aop.MethodBeforeAdvice接口:1. /*2. *Adviceinvokedbeforeamethodisinvoked.Suchadvicescannot3. *preventthemethodcallproceeding,unlesstheythrowaThrowable.4. */5. publicinterfaceMethodBeforeAdviceextendsBeforeAdvice6. 7. /*8. *Callbackbeforeagivenmethodisinvoked.9. *parammethodmethodbeinginvoked10. *paramargsargumentstothemethod11. *paramtargettargetofthemethodinvocation.Maybenull.12. *throwsThrowableifthisobjectwishestoabortthecall.13. *Anyexceptionthrownwillbereturnedtothecallerifits14. *allowedbythemethodsignature.Otherwisetheexception15. *willbewrappedasaruntimeexception.16. */17. voidbefore(Methodmethod,Objectargs,Objecttarget)throwsThrowable;18. After Advice实现org.springframework.aop.AfterReturningAdvice接口:1. /*2. *Afterreturningadviceisinvokedonlyonnormalmethodreturn,notifan3. *exceptionisthrown.Suchadvicecanseethereturnvalue,butcannotchangeit.4. */5. publicinterfaceAfterReturningAdviceextendsAfterAdvice6. /*7. *Callbackafteragivenmethodsuccessfullyreturned.8. *paramreturnValuethevaluereturnedbythemethod,ifany9. *parammethodmethodbeinginvoked10. *paramargsargumentstothemethod11. *paramtargettargetofthemethodinvocation.Maybenull.12. *throwsThrowableifthisobjectwishestoabortthecall.13. *Anyexceptionthrownwillbereturnedtothecallerifits14. *allowedbythemethodsignature.Otherwisetheexception15. *willbewrappedasaruntimeexception.16. */17. voidafterReturning(ObjectreturnValue,Methodmethod,Objectargs,Objecttarget)throwsThrowable;18. Around Advice需要实现ercept.MethodInterceptor接口:1. /*2. *Theusershouldimplementthelink#invoke(MethodInvocation)3. *methodtomodifytheoriginalbehavior.E.g.thefollowingclass4. *implementsatracinginterceptor(tracesallthecallsonthe5. *interceptedmethod(s):6. *7. *8. *classTracingInterceptorimplementsMethodInterceptor9. *Objectinvoke(MethodInvocationi)throwsThrowable10. *System.out.println(method+i.getMethod()+iscalledon+11. *i.getThis()+withargs+i.getArguments();12. *Objectret=ceed();13. *System.out.println(method+i.getMethod()+returns+ret);14. *returnret;15. *16. *17. */18. publicinterfaceMethodInterceptorextendsInterceptor19. 20. /*21. *Implementthismethodtoperformextratreatmentsbeforeand22. *aftertheinvocation.Politeimplementationswouldcertainly23. *liketoinvokelinkJoinpoint#proceed().24. *25. *paraminvocationthemethodinvocationjoinpoint26. *returntheresultofthecalltolink27. *Joinpoint#proceed(),mightbeinterceptedbythe28. *interceptor.29. *30. *throwsThrowableiftheinterceptorsorthe31. *target-objectthrowsanexception.*/32. Objectinvoke(MethodInvocationinvocation)throwsThrowable;33. 类前面的注释说明了该方法的使用,就是要在invoke()方法中调用MethodIceed(),将执行传给下一个Interceptor,最终执行目标方法。在proceed()方法前后加操作,到达Aroud advice的作用。在Aspect定义好后,就需要在bean定义文件中进行配置,通过org.springframework.aop.framework.ProxyFactoryBean的配置,指出接口、目标类和Aspect。如下:1. 2. 3. 4. 5. 6. 7. 8. 9. throwAdvice10. !-还可以继续加,如11. logBeforeAdvice-12. 13. 14. 补充下,其中的目标类、解释器(Aspect)都要在Bean定义文件中先进行定义,然后才可以引用的。我们使用时,代码如下:1. IHellohelloProxy=(IHello)context.getBean(helloProxy);2. helloProxy.hello();在Bean定义文件中配置的Aspect就会在合适的joinPoint应用到目标方法上。补:IHello接口中有hello()方法,HelloSpeaker类实现了IHello接口。以免糊涂,给个实例如下,功能是在hello()方法前后加入日志记录。1. publicclassAroundAdviceimplementsMethodInterceptor2. privateLoggerlogger=3. Logger.getLogger(this.getClass().getName();4. Override5. publicObjectinvoke(MethodInvocationmethodInvocation)throwsThrowable6. 7. logger.log(Level.INFO,methodstarts.+methodInvocation.getMethod();8. Objectresult=methodIceed();9. logger.log(Level.INFO,methodends.+metho
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 标准化员工绩效考核模板
- 财务管理报表生成工具轻松掌握财务数据
- 企业文化建设与推广实施方案工具箱
- 2025海南省监狱管理局(海南省戒毒管理局)遴选人力资源服务公司协助开展招聘警务辅助人员工作模拟试卷及答案详解(各地真题)
- 2025广东佛山市商务局招考专业技术雇员1人考前自测高频考点模拟试题附答案详解
- 2025安徽庐江县乡村振兴投资有限公司招聘10人考前自测高频考点模拟试题及1套参考答案详解
- 2025年江西农商联合银行金融科技人才招聘25人考前自测高频考点模拟试题及完整答案详解1套
- 2025年临沂莒南县教体系统部分事业单位公开招聘教师(1名)模拟试卷及答案详解(有一套)
- 2025第十三届贵州人才博览会黔东南州企事业单位招聘模拟试卷及1套参考答案详解
- 2025广东清远市英德市招聘教师222人模拟试卷及答案详解(必刷)
- 资深船长:18天终于把船舶抵达美国加州的注意事项理清了
- 植物生理学第七章细胞信号转导
- DB32T-乡镇农产品质量安全监管机构建设规范编制说明
- CJ/T 124-2016 给水用钢骨架聚乙烯塑料复合管件
- 一例晚期直肠癌多发转移患者镇痛治疗病例分享
- 七年级数学(上)有理数混合运算100题(含答案)
- 公共事业行业财务人员培训:财务管理与预算编制
- 《工业互联网技术导论》 课件全套 第1-10章 智能制造与工业互联网-工业人工智能
- 定向钻机弯头调整方法
- 离婚协议书(完整版)WORDx(二篇)
- 加油站应急情况处理技巧
评论
0/150
提交评论