陸續(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ù)免費閱讀

下載本文檔

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

文檔簡介

AOPAOPAOP面向對象 面向對象AOPAOPAOPAOPpublicclassMyAdvicepublicvoidbeforeAdvice(){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進行動 Bean= ,Methodmethod,Object[]args)->{Objectresult=method.invoke(bean,args);//執(zhí)行目標myAdvice.af returnresult;});//返 Bean;returnbean;publicvoidsetApplicationContext(ApplicationContextapplicationContext)throws ceptionthis.applicationContext=AOPAOPAOPAOPAOP

基于注解配置的基于AOP 式事務控 //對Bean進行動 Bean= ,Methodmethod,Object[]args)->{Objectresult=methodinvoke(bean,args);//執(zhí)行目標 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目標方法執(zhí)行之前執(zhí)后置通<aop:after-returning目標方法執(zhí)行之后執(zhí)行,目標方法異常時,不在執(zhí)環(huán)繞通<aop:around目標方法執(zhí)行前后執(zhí)行,目標方法異常時,環(huán)繞后方法不在執(zhí)異常通<aop:after-throwing目標方法拋出異常時執(zhí)最終通<aop:after不管目標方法是否有異常,最終都會執(zhí)publicvoidpublicvoidaround(ProceedingJoinPointjoinPoint)throwsThrowable}<<aop:aroundmethod="around"pointcut-publicvoidpublicvoid}<aop:after-throwingmethod="afterThrowing"pointcut-publicpublicvoid}<aop:aftermethod="after"pointcut-參數(shù)類連接點對象,任何通知都可使用,可以獲得當前目標對象、目標方法參數(shù)等信JoinPoint子類對象,主要是在環(huán)繞通知中執(zhí)行proceed(),進而執(zhí)行目標方異常對象,使用在異常通知中,需要在配置文件 publicvoidpublicvoid通知方法名稱(JoinPoint//獲得目標方法的參//獲得精確的切點表達式信}publicObjectaround(ProceedingJoinPointjoinPoint)throwsThrowable{publicObjectaround(ProceedingJoinPointjoinPoint)throwsThrowable{Objectresult=joinPceed();//執(zhí)行目標方法returnresult;//返回目標方法返回}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動技目標類有接口,是基于接口動態(tài)生成實現(xiàn)類對目標類有接口的情況下,默認方Cglib動技類有接口時,手動配置<aop:config-Targettarget=Targettarget=newTarget();//目標對象Advicesadvices=newAdvices();//通知對象EnhancerenhancernewEnhancer();//增強器對象enhancer.setCallback((MethodInterceptor)(o,method,objects,methodObjectresult=method.invoke(target,objects);return)-> Target =(Target)Stringresult= AOP

基于注解配置的基于AOP 式事務控基于注解配置的<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 式事務控事務控制方編程式事務控式事務控事務控制相關平臺事務管理器事務定義封裝事務的級別、行為、過期時間等屬性信事務狀態(tài)當前事務的狀態(tài)信息,如果事務是否提交、是否回滾、是否有回滾點 基于 <<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級別,取決于當前數(shù)據(jù)級別,例如MySQL級別是A事務可以到B事務尚未提交的事務記錄,不能解決任何并發(fā)問題,安全性最低,性能最 基于 <tx:methodname="select*"read-<tx:methodname="find*"read-<tx:methodname="select*"read-only="true"事務行REQUIRED(默認值A調(diào)用B,B需要事務,如果A有事務B就加入A的事務中,如果A沒有事務,B就自己創(chuàng)建一個事A調(diào)用B,B需要新事務,如果A有事務就掛起,B自己創(chuàng)建一個新的事A調(diào)用B,B有無事務無所謂,A有事務就加入到A事務中,A無事務B就以非事務方式執(zhí)A調(diào)用B,B以無事務方式執(zhí)行,A

溫馨提示

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

評論

0/150

提交評論