国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

CGLib動態(tài)代理

2019-11-11 06:38:04
字體:
供稿:網(wǎng)友
代碼package com.itheima.sPRing.cglibproxy;public class PersonDaoImpl{ public void savePerson() { System.out.println("save person"); }}package com.itheima.spring.cglibproxy;public class Transaction { public void beginTransaction(){ System.out.println("begin transcation"); } public void commit() { System.out.println("commit"); }}package com.itheima.spring.cglibproxy;import java.lang.reflect.Method;import net.sf.cglib.proxy.Enhancer;import net.sf.cglib.proxy.MethodInterceptor;import net.sf.cglib.proxy.MethodProxy;public class MyInterceptor implements MethodInterceptor{ private Transaction transaction; private Object target; public MyInterceptor(Object target, Transaction transaction ) { super(); this.transaction = transaction; this.target = target; } public Object createProxy(){ //代碼增強類 Enhancer enhancer = new Enhancer(); enhancer.setCallback(this);//參數(shù)為攔截器 enhancer.setSuperclass(target.getClass());//生成的代理類的父類是目標(biāo)類 return enhancer.create(); } @Override public Object intercept(Object object, Method method, Object[] arg2, MethodProxy arg3) throws Throwable { this.transaction.beginTransaction(); method.invoke(target); this.transaction.commit(); return null; }}package com.itheima.spring.cglibproxy;import org.junit.Test;/** * 通過cglib產(chǎn)生的是代理對象,代理類是目標(biāo)類的子類 * @author xx * */public class CGLibProxyTest { @Test public void testCGlib(){ Object target = new PersonDaoImpl(); Transaction transaction = new Transaction(); MyInterceptor interceptor = new MyInterceptor(target,transaction); PersonDaoImpl personDaoImpl = (PersonDaoImpl)interceptor.createProxy(); personDaoImpl.savePerson(); }}思考1、JDK動態(tài)代理與CGLib動態(tài)代理的比較?
①Java動態(tài)代理是利用反射機制生成一個實現(xiàn)代理接口的匿名類,在調(diào)用具體方法前調(diào)用InvokeHandler來處理。 而cglib動態(tài)代理是利用asm開源包,對代理對象類的class文件加載進(jìn)來,通過修改其字節(jié)碼生成子類來處理。
②JDK動態(tài)代理只能對實現(xiàn)了接口的類生成代理,CGLIB是針對類實現(xiàn)代理,主要是對指定的類生成一個子類,覆蓋其中的方法 因為是繼承,所以該類或方法最好不要聲明成final
③JDK代理是不需要以來第三方的庫,只要要JDK環(huán)境就可以,而cglib需要第三方j(luò)ar包
④CGLib創(chuàng)建的動態(tài)代理對象性能比JDK創(chuàng)建的動態(tài)代理對象的性能高,但是CGLib在創(chuàng)建代理對象時所花費的時間卻比JDK多得多,所以對于單例的對象,因為無需頻繁創(chuàng)建對象,用CGLib合適,反之,使用JDK方式要更為合適一些。
2、在Spring中何時用到JDK或者CGLib實現(xiàn)的AOP?
①如果目標(biāo)對象實現(xiàn)了接口,默認(rèn)情況下會采用JDK的動態(tài)代理實現(xiàn)AOP
②如果目標(biāo)對象實現(xiàn)了接口,可以強制使用CGLIB實現(xiàn)AOP
③如果目標(biāo)對象沒有實現(xiàn)了接口,必須采用CGLIB庫,spring會自動在JDK動態(tài)代理和CGLIB之間轉(zhuǎn)換
3、如何通過調(diào)試查看當(dāng)前用到的是JDK還是CGLib?CGLib的標(biāo)志: JDK的標(biāo)志:4、如何強制用CGLib實現(xiàn)AOP?
①添加CGLIB庫,SPRING_HOME/cglib/*.jar
②在spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"/>
5、cglib的應(yīng)用?
廣泛的被許多AOP的框架使用,例如spring AOP和dynaop。hibernate使用CGLIB來代理單端single-ended(多對一和一對一)關(guān)聯(lián)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 平武县| 阳泉市| 乳山市| 通城县| 广西| 邻水| 黄骅市| 石柱| 罗源县| 海城市| 东莞市| 家居| 黑河市| 鄱阳县| 布拖县| 丘北县| 民权县| 郯城县| 乐东| 平果县| 博罗县| 九龙县| 三穗县| 宝应县| 建宁县| 尼木县| 周宁县| 吴旗县| 靖安县| 高雄市| 牙克石市| 独山县| 比如县| 兴义市| 碌曲县| 平安县| 电白县| 广德县| 景谷| 方城县| 克东县|