spring4中文技术手册==.docx_第1页
spring4中文技术手册==.docx_第2页
spring4中文技术手册==.docx_第3页
spring4中文技术手册==.docx_第4页
spring4中文技术手册==.docx_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论