陸續(xù)講義2一套吃透spring課程aop部分_第1頁
陸續(xù)講義2一套吃透spring課程aop部分_第2頁
陸續(xù)講義2一套吃透spring課程aop部分_第3頁
陸續(xù)講義2一套吃透spring課程aop部分_第4頁
陸續(xù)講義2一套吃透spring課程aop部分_第5頁
已閱讀5頁,還剩84頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

AOPAOPAOP面向?qū)ο?面向?qū)ο驛OPAOPAOPAOPpublicclassMyAdvicepublicvoidbeforeAdvice(){System.out.println("beforeAdvice...");}publicvoidaf dvice...");}}AOPpublicclassMockAopBeanPostProcessorimplementsBeanPostProcessor,ApplicationContextAwareprivateApplicationContextapplicationContext;//注入Spring容器對publicObjectpostProcessAfterInitialization(Objectbean,StringbeanName)throws {MyAdvicemyAdvice=applicationContext.getBean(MyAdvice.class);//獲得Advice對象StringpackageName=bean.getClass().getPackage().getName();//對Bean進(jìn)行動 Bean= ,Methodmethod,Object[]args)->{Objectresult=method.invoke(bean,args);//執(zhí)行目標(biāo)myAdvice.af returnresult;});//返 Bean;returnbean;publicvoidsetApplicationContext(ApplicationContextapplicationContext)throws ceptionthis.applicationContext=AOPAOPAOPAOPAOP

基于注解配置的基于AOP 式事務(wù)控 //對Bean進(jìn)行動 Bean= ,Methodmethod,Object[]args)->{Objectresult=methodinvoke(bean,args);//執(zhí)行目標(biāo) returnresult;});//返 Bean;returnbean;publicinterfacepublicinterfaceUserServicevoidvoid}publicclassUserServiceImplimplementsUserServicepublicvoidshow1(){}publicvoidshow2(){publicclassMyAdvicepublicvoidbeforeAdvice(){}publicvoid }}<beanid="userService"<beanid=“myAdvice"<aop:pointcutid="myPointcut"expression="execution(void<aop:aspect<aop:beforemethod="beforeAdvice"pointcut- <aop:after-returning dvice"pointcut- xml方式AOP<aop:pointcutid="myPointc<aop:aspect<aop:beforeut"expression="execution(void>foreAdvice"pointcut-dvice" 修飾符]返回值類型包名.類名.方法名(參數(shù)*;表表修飾符、無返回值、.itheima.aop包下的TargetImpl類的無參方法execution(publicvoid execution(*com.itheima.aop.TargetImpl.*(..)) execution(*com.itheima.aop.*.*(..)) execution(*com.itheima.aop..*.*(..))//表示任意包中的任意類的任意方execution(*通知名配置方執(zhí)行時通知名配置方執(zhí)行時前置通<aop:before目標(biāo)方法執(zhí)行之前執(zhí)后置通<aop:after-returning目標(biāo)方法執(zhí)行之后執(zhí)行,目標(biāo)方法異常時,不在執(zhí)環(huán)繞通<aop:around目標(biāo)方法執(zhí)行前后執(zhí)行,目標(biāo)方法異常時,環(huán)繞后方法不在執(zhí)異常通<aop:after-throwing目標(biāo)方法拋出異常時執(zhí)最終通<aop:after不管目標(biāo)方法是否有異常,最終都會執(zhí)publicvoidpublicvoidaround(ProceedingJoinPointjoinPoint)throwsThrowable}<<aop:aroundmethod="around"pointcut-publicvoidpublicvoid}<aop:after-throwingmethod="afterThrowing"pointcut-publicpublicvoid}<aop:aftermethod="after"pointcut-參數(shù)類連接點對象,任何通知都可使用,可以獲得當(dāng)前目標(biāo)對象、目標(biāo)方法參數(shù)等信JoinPoint子類對象,主要是在環(huán)繞通知中執(zhí)行proceed(),進(jìn)而執(zhí)行目標(biāo)方異常對象,使用在異常通知中,需要在配置文件 publicvoidpublicvoid通知方法名稱(JoinPoint//獲得目標(biāo)方法的參//獲得精確的切點表達(dá)式信}publicObjectaround(ProceedingJoinPointjoinPoint)throwsThrowable{publicObjectaround(ProceedingJoinPointjoinPoint)throwsThrowable{Objectresult=joinPceed();//執(zhí)行目標(biāo)方法returnresult;//返回目標(biāo)方法返回}publicpublicvoidafterThrowing(JoinPointjoinPoint,Throwable}<<aop:after-throwingmethod="afterThrowing"pointcut-ref="myPointcut"publicinterfacepublicinterfaceAdvice}publicpublicclassAdvicesimplementsMethodBeforeAdvice,AfterReturningAdvicepublicvoidbefore(Methodmethod,Object[]objects,Objecto)throwsThrowable{System.out.println("ThisisbeforeAdvice...");}publicvoidafterReturning(Objecto,Methodmethod,Object[]objects,Objecto1)ThrowableSystem.out.println("ThisisafterReturnAdvice<aop:advisoradvice-ref="advices"pointcut="execution(void publicclasspublicclassMyMethodInterceptorimplementsMethodInterceptor{publicObjectinvoke(MethodInvocationmethodInvocation)throwsThrowable{Objectinvoke=return <aop:advisoradvice-ref=“myMethodInterceptor"pointcut="execution(void<aop:advisoradvice-ref="advices"pointcut="execution(void<aop:aspect<aop:beforemethod="before"pointcut="execution(void 類需要實現(xiàn)Advice的子功能接publicpublicclassAdvicesimplementsMethodBeforeAdvicepublicvoidbefore(Methodmethod,Object[]objects,Objecto)throwsThrowable{System.out.println("ThisisbeforeAdvice...");}publicvoidafterReturning(Objecto,Methodmethod,Object[]objects,Objecto1)ThrowableSystem.out.println("ThisisafterReturnAdviceaspect:publicclassAdvicespublicvoidbefore()System.out.println("ThisisbeforeAdvice}publicvoidafterReturning()System.out.println("ThisisafterReturnAdvice xml方式AOPhttp\http\this.registerBeanDefinitionParser("config",newthis.registerBeanDefinitionParser("config",new Creator的上上級父 publicObjectpostProcessAfterInitialization(@NullableObjectbean,StringbeanName)if(bean!=null)ObjectcacheKey=this.getCacheKey(bean.getClass(),if References.remove(cacheKey)!=bean) returnthis.wrapIfNecessary(bean,beanName,}}return}==>this==>this.wrapIfNecessary(bean,beanName,==> = (參數(shù)省略 ==> ==>()是一個接口方法,實現(xiàn)類有兩個,如下截Instance(classLoader,xiedInterfaces,技使用條配置方JDK動技目標(biāo)類有接口,是基于接口動態(tài)生成實現(xiàn)類對目標(biāo)類有接口的情況下,默認(rèn)方Cglib動技類有接口時,手動配置<aop:config-Targettarget=Targettarget=newTarget();//目標(biāo)對象Advicesadvices=newAdvices();//通知對象EnhancerenhancernewEnhancer();//增強(qiáng)器對象enhancer.setCallback((MethodInterceptor)(o,method,objects,methodObjectresult=method.invoke(target,objects);return)-> Target =(Target)Stringresult= AOP

基于注解配置的基于AOP 式事務(wù)控基于注解配置的<bean<bean<!--配置aopget"ices">ct>dmethod="around"pointcut="execution(*publicclassTargetImplimplementspublicvoidshow()System.out.println("showTarget}}publicclassAnnoAdvicepublicvoidaround(ProceedingJoinPointjoinPoint)throwsThrowable{}} publicclassAnnoAdvice@Around("execution(*com.itheima.aop.*.*(..))"http://第二publicvoidaround(ProceedingJoinPointjoinPoint)throwsThrowable{ publicclassAnnoAdvice@Around("execution(*com.itheima.aop.*.*(..))"http://第二publicvoidaround(ProceedingJoinPointjoinPoint)throwsThrowable{ publicclassApplicationContextConfig}基于注解配置的@Before("execution(*com.itheima.aop.*.*(..))")publicvoidbefore(JoinPointjoinPoint){}@AfterReturning("execution(*com.itheima.aop.*.*(..))")publicvoidAfterReturning(JoinPointjoinPoint){}@Around("execution(*publicvoidaround(ProceedingJoinPointjoinPoint)throwsThrowable@AfterThrowing("execution(*com.itheima.aop.*.*(..))")publicvoidAfterThrowing(JoinPointjoinPoint){}@After("execution(*com.itheima.aop.*.*(..))")publicvoidAfter(JoinPointjoinPoint){}publicclassAnnoAdvice@Pointcut("execution(*com.itheima.aop.*.*(..))")publicvoidpointcut(){}publicvoidbefore(JoinPointpublicclassAnnoAdvice@Pointcut("execution(*com.itheima.aop.*.*(..))")publicvoidpointcut(){}publicvoidbefore(JoinPointpublicvoidAfterReturning(JoinPoint//...省略其他代碼}基于注解配置的", ", Creator.class,registry,publicclassApplicationContextConfig}publicclassApplicationContextConfig} public@interface public@interface TargetClass()defaultboolean()default}AnnotationAwareAspectJAutoCreator這個類publicvoidregisterBeanDefinitions(AnnotationMetadataimportingClassMetadata,BeanDefinitionRegistryregistry){ }publicstatic CreatorIfNecessary(BeanDefinitionRegistryregistry)return CreatorIfNecessary(registry,}publicstatic CreatorIfNecessary(BeanDefinitionRegistryregistry,@NullableObjectsource){returnregisterOrEscalateApcAsRequired(AnnotationAwareAspectJAuto registry,source);}基于注解配置的AOP

基于注解配置的基于AOP 式事務(wù)控事務(wù)控制方編程式事務(wù)控式事務(wù)控事務(wù)控制相關(guān)平臺事務(wù)管理器事務(wù)定義封裝事務(wù)的級別、行為、過期時間等屬性信事務(wù)狀態(tài)當(dāng)前事務(wù)的狀態(tài)信息,如果事務(wù)是否提交、是否回滾、是否有回滾點 基于 <<beanid="accountService"<propertyname="accountMapper"<aop:advisoradvice-ref="Spring<aop:advisoradvice-ref="Spring提供類 基于 xmlns:tx="xmlns:tx=" <tx:adviceid="myAdvice"transaction-<tx:method<beanid="transactionManager"<propertyname="dataSource"<aop:advisoradvice-ref="myAdvice"pointcut="execution(*publicpublictformTransactionManagerextendsTransactionManagerTransactionStatusgetTransaction(@NullableTransactionDefinitionvar1)voidcommit(TransactionStatusvar1)throwsvoidrollback(TransactionStatusvar1)throws}<tx:methodname="方法名稱 基于AOP的基于xml 基于 *<<tx:adviceid="myAdvice"transaction-<!--精確匹配transferMoney方法<tx:method<tx:method<tx:method<tx:method<tx:method MITTED和REPEATABLE_READ級別,取決于當(dāng)前數(shù)據(jù)級別,例如MySQL級別是A事務(wù)可以到B事務(wù)尚未提交的事務(wù)記錄,不能解決任何并發(fā)問題,安全性最低,性能最 基于 <tx:methodname="select*"read-<tx:methodname="find*"read-<tx:methodname="select*"read-only="true"事務(wù)行REQUIRED(默認(rèn)值A(chǔ)調(diào)用B,B需要事務(wù),如果A有事務(wù)B就加入A的事務(wù)中,如果A沒有事務(wù),B就自己創(chuàng)建一個事A調(diào)用B,B需要新事務(wù),如果A有事務(wù)就掛起,B自己創(chuàng)建一個新的事A調(diào)用B,B有無事務(wù)無所謂,A有事務(wù)就加入到A事務(wù)中,A無事務(wù)B就以非事務(wù)方式執(zhí)A調(diào)用B,B以無事務(wù)方式執(zhí)行,A

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論