AOP。。。
一、代理目標(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é)
- 上一篇: Mysql跨数据库事务
- 下一篇: OkHttp从使用到源代码分析(2)-请