spring4中文技术手册_第1页
spring4中文技术手册_第2页
spring4中文技术手册_第3页
spring4中文技术手册_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

spring4spring4中文技术手册在《静态代理和动态代理》中提到了面对方面编程,主要就是基于动态代理。单独抽象出非业务的功能,效劳于某些业务方法。SpringAdvice,分别为:BeforeAdvice,AfterReturningAdvice,AroundAdvice,AfterthrowingAdvice。都是方法级别的,就是在某个方法执行前后插入一些非业务的操作,如打日志或者推断权限等。advice的实现,springxmlannotation(注释)。BeforeAdviceAfterAdviceAroundAdvice则可以在目标方法执行前后同时加上相关效劳;ThrowAdvice是在特别发生后执行某些操作。1.AdviceAspectSpring接口。BeforeAdviceorg.springframework.aop.MethodBeforeAdvice接口:1. 1. /**2.*Adviceinvokedbeforeamethodisinvoked.Suchadvicescannot3.*preventthemethodcallproceeding,unlesstheythrowaThrowable.4.*/5. publicinterfaceMethodBeforeAdviceextendsBeforeAdvice{6.7./**8.*Callbackbeforeagivenmethodisinvoked.9.*@parammethodmethodbeinginvoked10.*@paramargsargumentstothemethod11.*@paramtargettargetofthemethodinvocation.Maybe<code>null</code>.12.*@throwsThrowableifthisobjectwishestoabortthecall.13.*Anyexceptionthrownwillbereturnedtothecallerifit”s14.*allowedbythemethodsignature.Otherwisetheexception15.*willbewrappedasaruntimeexception.16.*/17.voidbefore(Methodmethod,Object[]args,Objecttarget)throwsThrowable;18.}AfterAfterAdviceorg.springframework.aop.AfterReturningAdvice接口:1. /**2.2.*Afterreturningadviceisinvokedonlyonnormalmethodreturn,notifan3.*exceptionisthrown.Suchadvicecanseethereturnvalue,butcannotchangeit.4.*/5. publicinterfaceAfterReturningAdviceextendsAfterAdvice{6./**7.*Callbackafteragivenmethodsuccessfullyreturned.8.*@paramreturnValuethevaluereturnedbythemethod,ifany9.*@parammethodmethodbeinginvoked10.*@paramargsargumentstothemethod11.*@paramtargettargetofthemethodinvocation.Maybe<code>null</code>.12.*@throwsThrowableifthisobjectwishestoabortthecall.13.*Anyexceptionthrownwillbereturnedtothecallerifit”s14.*allowedbythemethodsignature.Otherwisetheexception15.*willbewrappedasaruntimeexception.16.*/17.voidafterReturning(ObjectreturnValue,Methodmethod,Object[]args,Objecttarget)throwsThrowable;18.}AroundAroundAercept.MethodInterceptor接口:1. /**2.*<p>Theusershouldimplementthe{@link#invoke(MethodInvocation)}3.*methodtomodifytheoriginalbehavior.E.g.thefollowingclass4.*implementsatracinginterceptor(tracesallthecallsonthe5.*interceptedmethod(s)):6.*7.*<preclass=code>8.*classTracingInterceptorimplementsMethodInterceptor{9.* Objectinvoke(MethodInvocationi)throwsThrowable{10. *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. 15. * }16. *}17. *</pre>*/18.publicinterfaceMethodInterceptorextendsInterceptor{19.20./**21.*Implementthismethodtoperformextratreatmentsbeforeand22.*aftertheinvocation.Politeimplementationswouldcertainly23.*liketoinvoke{@linkJoinpoint#proceed}.24.*25.*@paraminvocationthemethodinvocationjoinpoint26.*@returntheresultofthecallto{@link27.*Joinpoint#proceed},mightbeinterceptedbythe28.*interceptor.29.*30.*@throwsThrowableiftheinterceptorsorthe31.*target-objectthrowsanexception.*/32.Objectinvoke(MethodInvocationinvocation)throwsThrowable;33.}类前面的注释说明白该方法的使用,就是要在类前面的注释说明白该方法的使用,就是要在invoke方法中调用MethodIceed,将执行传给下一个Interceptor,最终执行目标方法。在proceed方法前后加操作,到达Aroudadvice的作用。Aspectbeanorg.springframework.aop.framework.ProxyFactoryBeanAspect。如下:1. <beanid=“helloProxy“class=“org.springframework.aop.framework.ProxyFactoryBean“>2.业务接口-->3.<propertyname=“proxyInterfaces“value=“spring.advice.IHello“/>4.实现该接口的目标类-->5.<propertyname=“target“ref=“helloSpeaker“/>6.Aspect7.<propertyname=“interceptorNames“>8.<list>9.<value>throwAdvice</value>10.还可以连续加,如11.<value>logBeforeAdvice</value>-->12.12.</list>13.</property>14.</bean>补充下,其中的目标类、解释器(Aspect)Bean定义文件中先进展定义,然后才可以引用的。我们使用时,代码如下:IHelloIHellohelloProxy=(IHello)context.getBean(“helloProxy“);helloProxy.hello;BeanBeanAspectjoinPoint应用到目标方法上。补:IHellohello方法,HelloSpeakerIHello接口。hello方法前后参与日志记录。1. publicclassAroundAdviceimplementsMethodInterceptor{2.privateLoggerlogger=3.4.@Override5.publicObjectinvoke(MethodInvocationmethodInvocation)throwsThrowable{6.7.logger.log(Level.INFO,“methodstarts...“+methodInvocation.getMethod);8.result=methodIceed;9.ends...“+methodInvocation.getMethod);10.11.returnresult;12.}13.}配置:<?xml<?xmlversion=“1.0“encoding=“UTF-8“?>3.4.>5.<beanid=“helloSpeaker“class=“spring.advice.HelloSpeaker“/>IHelloIHellohelloProxy=(IHello)context.getBean(“helloProxy“);helloProxy.hello(“前后加日志“);就会消灭如下结果:6.<beanid=“aroundAdvice“class=“spring.advice.AroundAdvice“/>7.8.<beanid=“helloProxy“class=“org.springframework.aop.framework.ProxyFact

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论