日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AOP。。。

發(fā)布時(shí)間:2024/1/18 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AOP。。。 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、代理目標(biāo)(target)

誰將被其他對(duì)象代理,誰就是代理目標(biāo)

二、代理對(duì)象(proxy)

誰將代理其他對(duì)象,誰就是代理對(duì)象

三、連接點(diǎn)(Join Point)

1.執(zhí)行點(diǎn)

任意一個(gè)可以執(zhí)行的方法都可以當(dāng)作一個(gè)執(zhí)行點(diǎn)

2.方位

方法執(zhí)行前(before)

方法執(zhí)行前后(around)

方法拋出異常后(after-throw)

方法正常返回后(after-return)

方法執(zhí)行后(after)

3.連接點(diǎn)

連接點(diǎn)=執(zhí)行點(diǎn)+方位

四、切點(diǎn)(Pointcut)

對(duì)連接點(diǎn)進(jìn)行篩選的條件

要指定執(zhí)行點(diǎn)和方位信息

確定在哪加入代碼

五、Advice

在指定的切點(diǎn)所選擇的連接點(diǎn)加入的代碼就是Advice

確定加入什么代碼

六、切面(Aspect)

切面(Aapect)=切點(diǎn)(Pointcut)+Advice

在哪里加,加什么代碼(在哪里加什么)

示例代碼:

第一步:創(chuàng)建Cat.java和CatAdvices.java類

Cat.java類:

public class Cat { private String name; public void eat(String food){ System.out.println(this.name+"吃"+food); } public int div(int a,int b){ int c=a/b; return c; }

public String getName() {return name; }public void setName(String name) {this.name = name; }

}

CatAdvices.java類:

public class CatAdvices { public void before(JoinPoint joinPoint){ Signature signature=joinPoint.getSignature();// 獲取 連接點(diǎn) 對(duì)應(yīng)的 方法 的 簽名 System.out.println("signature"+signature); int mod=signature.getModifiers();// 獲得方法的 修飾符 ( 以整數(shù)形式返回 ) String modifier= Modifier.toString(mod);// 將 整數(shù)形式表示的 修飾符 解析為 字符串 形式 System.out.println("modifier"+modifier); String modName=signature.getName(); // 獲取 方法名稱 System.out.println("modName"+modName); Object[] args=joinPoint.getArgs(); System.out.println("arguments"+Arrays.toString(args)); System.out.println("【"+modName+"】方法即將執(zhí)行!"); } public Object round(ProceedingJoinPoint joinPoint) throws Throwable{ String name=joinPoint.getSignature().getName(); System.out.println("開始為"+name+"計(jì)時(shí)"); long begin=System.nanoTime(); Object result=joinPoint.proceed(); long end=System.nanoTime(); System.out.println("為"+name+"計(jì)時(shí)結(jié)束"); System.out.println("["+name+"耗時(shí)"+(end-begin)+"ns]"); return result;// 返回 由 被攔截的方法執(zhí)行后 所返回的值 }

public void afterReturn(JoinPoint joinPoint,Object xxx){System.out.println("【"+joinPoint.getSignature().getName()+"】方法執(zhí)行后返回了"+"【"+xxx+"】");} public void afterThrow(JoinPoint joinPoint,Throwable ex){System.out.println("【"+joinPoint.getSignature().getName()+"】方法執(zhí)行時(shí)拋出了"+ex);} public void after(JoinPoint joinPoint){// 獲取 連接點(diǎn) 對(duì)應(yīng)的 方法 的 簽名Signature signature=joinPoint.getSignature();String mothodName=signature.getName();System.out.println("【"+mothodName+"】執(zhí)行結(jié)束!"); }

}

第二步:

aop-schema.xml

<!-- 確定需要 添加的代碼所在的 bean --> <bean id="catAdvices" class="com.itlaobing.aop.schema.CatAdvices"/> <!-- 確定代理目標(biāo)(誰將被代理) --> <bean id="cat" class="com.itlaobing.aop.schema.Cat" p:name="琪琪"/> <!--提供AOP的配置--> aop:config <aop:aspect ref="catAdvices"> <aop:pointcut id="pc" expression="execution(* com..schema.Cat.*(..))"/> <aop:before method="before" pointcut-ref="pc"/> <aop:after method="after" pointcut-ref="pc"/> <aop:after-returning method="afterReturn" pointcut-ref="pc" returning="xxx"/> <aop:after-throwing method="afterThrow" pointcut-ref="pc" throwing="ex"/> <aop:around method="round" pointcut-ref="pc"/> </aop:aspect> </aop:config> </beans>

第三步

public class CatTest1 { public static void main(String[] args) { String configLocation="com/itlaobing/aop/schema/aop-schema. AbstractApplicationContext container=new ClassPathXmlApplicationContext(configLocation); Object proxy=container.getBean("cat"); System.out.println(proxy.getClass().getName()); Class<?> clazz=proxy.getClass().getSuperclass(); System.out.println(clazz); if(proxy instanceof Cat){ Cat cat=(Cat)proxy; cat.eat("魚"); System.out.println("---------------------1-----------------------"); String catName= cat.getName(); System.out.println(catName); System.out.println("---------------------2-----------------------"); int result=cat.div(100,2); System.out.println(result); System.out.println("---------------------3-----------------------"); result=cat.div(100,0); System.out.println(result); } container.close();

}

}

轉(zhuǎn)載于:https://my.oschina.net/u/4008495/blog/2986468

總結(jié)

以上是生活随笔為你收集整理的AOP。。。的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。