SpringAOP simple guide.docx

上传人:b****4 文档编号:5423805 上传时间:2022-12-16 格式:DOCX 页数:8 大小:198.86KB
下载 相关 举报
SpringAOP simple guide.docx_第1页
第1页 / 共8页
SpringAOP simple guide.docx_第2页
第2页 / 共8页
SpringAOP simple guide.docx_第3页
第3页 / 共8页
SpringAOP simple guide.docx_第4页
第4页 / 共8页
SpringAOP simple guide.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

SpringAOP simple guide.docx

《SpringAOP simple guide.docx》由会员分享,可在线阅读,更多相关《SpringAOP simple guide.docx(8页珍藏版)》请在冰豆网上搜索。

SpringAOP simple guide.docx

SpringAOPsimpleguide

SpringAOPSimpleGuide

AdrianZhong

Version1.0

July26,2011

HPConfidential

SimpleAOPIntroduction

Acommonobject-orientedtechniqueforreusingcommonfunctionalityistoapplyinheritanceordelegation.Butinheritancecanleadtoabrittleobjecthierarchyifthesamebaseclassisusedthroughoutanapplication,anddelegationcanbecumbersomebecausecomplicatedcallstothedelegateobjectmayberequired.

Concepts

SomeTerms

Advice

InAOPterms,thejobofanaspectiscalledadvice.

Joinpoint

Yourapplicationmayhaveaplentyofopportunitiesforadvicetobeapplied.Theseopportunitiesareknownasjoinpoints.

Pointcut

Anaspectdoesn’tnecessarilyadvisealljoinpointsinanapplication.Pointcutshelpnarrowdownthejoinpointsadvisedbyanaspect.

Aspect

Anaspectisthemergerofadviceandpointcuts.

Weaving

Weavingistheprocessofapplyingaspectstoatargetobjecttocreateanew,proxied

object.

InSpring,aspectsarewovenintoSpring-managedbeansatruntimebywrappingthemwithaproxyclass.

SpringAOPconfiguration

1.Commonspringproxybean

Thespringcontextfile

Configurationjustlikeothernon-proxybeans.TheinterceptorNamespropertytellsProxyFactoryBeanwhichadvisorstoapplytotheproxiedbean.ThisOnlyuncomfortableisthattherealtargetbeanisnottheonewegetwhenwewannauseandgetformthespringApplicationContext.(Inanotherword,theinjectionandtheusageisnottransparent)

Advice

TheadvicesorcalledinterceptorsshouldinheritedfromMethodBeforeAdvice,MethodAfterAdviceetc.

Thiskindadivceiseasytouse,somehowliketheJDKDynamicProxy’sInvocationHandler.

2.AOPtagproxybean

Thespringcontextfile

Moreeasier,butneedtoaddtheAOPtagschema.Well,that’snotthecase.Exciting,theusageoftheapplicationfinallybecometransparenttothedevelopernow!

(Ifyouwanttoknowmoreaboutthepointcutconfigurationblew,pleasereferencetotheAspectJtoseemoreinfo)

Advice

PurePOJO,feelsgood~

3.Aopannotation

Thespringcontextfile

Thesimplest,easiest…whatcanweexpectmore…

Aspect

AnnotationedPOJOAOPtagproxybeanxmlconfiguration,butmappingthepointcutsandtheadvicesusingtheannotation.

Other

Weavingtime

Asmentionabove,weavingtimecouldbedifferentthroughtheobject’slifetime.

1.CompileTime

Itworkswhencompiletheclass,itneedaspecialcompilewhichcouldmergetheAspectintotheclassfiledirectlyduringthecompiletime.

Example:

AspectJ

2.ApplicationRuntime

Thiscansimplydividedintotwophases.Classloadingtimeandtheprogramruntime.

Example:

Spring(runtime),AspectJ(specialClassLoader)

ProxiedClasses

Springgeneratesproxiedclassesintwoways.

1.JDKDynamicProxy

2.CGLIB

Ifyourtargetobjectimplementsaninterface(s)thatexposestherequiredmethods,SpringwillusetheJDK’sjava.lang.reflect.Proxyclass.ThisclassallowsSpringtodynamicallygenerateanewclassthatimplementsthenecessaryinterfaces,weaveinanyadvice,andproxyanycallstotheseinterfacestoyourtargetclass.Ifyourtargetclassdoesnotimplementaninterface,SpringusestheCGLIBlibrarytogenerateasubclasstoyourtargetclass.

■Creatingaproxywithinterfacesisfavoredoverproxyingclasses,sincethisleadstoamorelooselycoupledapplication.Theabilitytoproxyclassesisprovidedsothatlegacyorthird-partyclassesthatdonotimplementinterfacescanstillbeadvised.Thisapproachshouldbetakenastheexception,nottherule.

■Methodsmarkedasfinalcannotbeadvised.Remember,Springgeneratesasubclasstoyourtargetclass.Anymethodthatneedstobeadvisedisoverriddenandadviceiswovenin.Thisisnotpossiblewithfinalmethods.

Nowletusjustlookattheproxiedclassgeneratedbythecontext.(Ijustcannotgetthebytestreamoftheproxiedclasses,sothearticlesbelowisjustmypersonalconsideration.Ifthereisanywrong,pleasepleaseletmeknow.)

BeginwiththeJDKDynamicProxyclass.

JDKDynamicProxyClass

Youmustnotifiedthattherearelotsofstaticclassmemberwithmeaninglessname,forpersonal,thesemarkscouldbeusedtomarkthespecificmethod,usedwhentheproxiedmethodiscalled.

GuessImplementation

AnotherCGLIBclass.

CGLIBProxyClass

Attachedthecodeofthesetests.

References

1.<>2nd.Edition.Aug.2007

2.<>

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 教育学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1