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