最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JDK動態(tài)代理與CGLib動態(tài)代理的區(qū)別對比

 更新時間:2019年02月11日 09:52:38   作者:邋遢的流浪劍客  
今天小編就為大家分享一篇關于JDK動態(tài)代理與CGLib動態(tài)代理的區(qū)別對比,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

案例:

public interface ForumService {
 void removeTopic(int topicId);
 void removeForum(int forumId);
}

對相關方法進行性能監(jiān)控

public class ForumServiceImpl implements ForumService {
 public void removeTopic(int topicId) {
 // PerformanceMonitor.begin("com.hand.proxy.ForumServiceImpl.removeTopic");
 System.out.println("模擬刪除Topic記錄:" + topicId);
 try {
  Thread.sleep(20);
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
 // PerformanceMonitor.end();
 }
 public void removeForum(int forumId) {
 // PerformanceMonitor.begin("com.hand.proxy.ForumServiceImpl.removeForum");
 System.out.println("模擬刪除Forum記錄:" + forumId);
 try {
  Thread.sleep(20);
 } catch (InterruptedException e) {
  e.printStackTrace();
 }
 // PerformanceMonitor.end();
 }
}

性能監(jiān)控實現(xiàn)類:

public class PerformanceMonitor {
 // 通過一個ThreadLocal保存與調用線程相關的性能監(jiān)視信息
 private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<MethodPerformance>();
 // 啟動對某一目標方法的性能監(jiān)視
 public static void begin(String method) {
 System.out.println("begin monitor...");
 MethodPerformance mp = new MethodPerformance(method);
 performanceRecord.set(mp);
 }
 public static void end() {
 System.out.println("end monitor...");
 MethodPerformance mp = performanceRecord.get();
 // 打印出方法性能監(jiān)視的結果信息
 mp.printPerformance();
 }
}

用于記錄性能監(jiān)控信息:

public class PerformanceMonitor {
 // 通過一個ThreadLocal保存與調用線程相關的性能監(jiān)視信息
 private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<MethodPerformance>();
 // 啟動對某一目標方法的性能監(jiān)視
 public static void begin(String method) {
 System.out.println("begin monitor...");
 MethodPerformance mp = new MethodPerformance(method);
 performanceRecord.set(mp);
 }
 public static void end() {
 System.out.println("end monitor...");
 MethodPerformance mp = performanceRecord.get();
 // 打印出方法性能監(jiān)視的結果信息
 mp.printPerformance();
 }
}

1、JDK動態(tài)代理

public class PerformanceMonitor {
 // 通過一個ThreadLocal保存與調用線程相關的性能監(jiān)視信息
 private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<MethodPerformance>();
 // 啟動對某一目標方法的性能監(jiān)視
 public static void begin(String method) {
 System.out.println("begin monitor...");
 MethodPerformance mp = new MethodPerformance(method);
 performanceRecord.set(mp);
 }
 public static void end() {
 System.out.println("end monitor...");
 MethodPerformance mp = performanceRecord.get();
 // 打印出方法性能監(jiān)視的結果信息
 mp.printPerformance();
 }
}
public class ForumServiceTest {
 @Test
 public void proxy() {
 ForumService forumService = new ForumServiceImpl();
 PerformanceHandler handler = new PerformanceHandler(forumService);
 ForumService proxy = (ForumService) Proxy.newProxyInstance(forumService.getClass().getClassLoader(),
  forumService.getClass().getInterfaces(), handler);
 proxy.removeForum(10);
 proxy.removeTopic(1012);
 }
}

得到以下輸出信息:

begin monitor...
模擬刪除Forum記錄:10
end monitor...
com.hand.proxy.ForumServiceImpl.removeForum花費21毫秒
begin monitor...
模擬刪除Topic記錄:1012
end monitor...
com.hand.proxy.ForumServiceImpl.removeTopic花費21毫秒

2、CGLib動態(tài)代理

 <!-- https://mvnrepository.com/artifact/cglib/cglib -->
 <dependency>
  <groupId>cglib</groupId>
  <artifactId>cglib</artifactId>
  <version>2.2.2</version>
 </dependency>
public class CglibProxy implements MethodInterceptor {
 private Enhancer enhancer = new Enhancer();
 public Object getProxy(Class clazz) {
 enhancer.setSuperclass(clazz);
 enhancer.setCallback(this);
 return enhancer.create();
 }
 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
 PerformanceMonitor.begin(obj.getClass().getName() + "." + method.getName());
 Object result = proxy.invokeSuper(obj, args);
 PerformanceMonitor.end();
 return result;
 }
}
public class ForumServiceTest2 {
 @Test
 public void proxy() {
 CglibProxy proxy = new CglibProxy();
 ForumServiceImpl forumService = (ForumServiceImpl) proxy.getProxy(ForumServiceImpl.class);
 forumService.removeForum(10);
 forumService.removeTopic(1023);
 }
}

1)、JDK和CGLib的區(qū)別

  • JDK動態(tài)代理只能對實現(xiàn)了接口的類生成代理,而不能針對類
  • CGLib是針對類實現(xiàn)代理,主要是對指定的類生成一個子類,覆蓋其中的方法(繼承)

2)、Spring在選擇用JDK還是CGLib的依據(jù)

  • 當Bean實現(xiàn)接口時,Spring就會用JDK的動態(tài)代理
  • 當Bean沒有實現(xiàn)接口時,Spring使用CGLib來實現(xiàn)
  • 可以強制使用CGLib(在Spring配置中加入<aop:aspectj-autoproxy proxy-target-class=“true”/>)

3)、JDK和CGLib的性能對比

  • 使用CGLib實現(xiàn)動態(tài)代理,CGLib底層采用ASM字節(jié)碼生成框架,使用字節(jié)碼技術生成代理類,在JDK1.6之前比使用Java反射效率要高。唯一需要注意的是,CGLib不能對聲明為final的方法進行代理,因為CGLib原理是動態(tài)生成被代理類的子類。
  • 在JDK1.6、JDK1.7、JDK1.8逐步對JDK動態(tài)代理優(yōu)化之后,在調用次數(shù)較少的情況下,JDK代理效率高于CGLib代理效率,只有當進行大量調用的時候,JDK1.6和JDK1.7比CGLib代理效率低一點,但是到JDK1.8的時候,JDK代理效率高于CGLib代理

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

相關文章

  • 深入理解 Java 中的 Switch 語句示例詳解

    深入理解 Java 中的 Switch 語句示例詳解

    在Java編程中,switch語句通過表達式值來執(zhí)行不同代碼塊,本文介紹switch語法、案例、注意事項,以及與if語句的對比,包括基本語法、關鍵字、表達式、case常量、break和default的使用,以及如何根據(jù)輸入的字符輸出星期、大小寫轉換、成績判斷和季節(jié)判斷等實際應用場景
    2024-10-10
  • Java模版引擎Freemarker

    Java模版引擎Freemarker

    FreeMarker是一個模板引擎,一個基于模板生成文本輸出的通用工具,使用純Java編寫 FreeMarker被設計用來生成HTML Web頁面,特別是基于MVC模式的應用程序
    2016-04-04
  • 使用log4j2自定義配置文件位置和文件名(附log4j2.xml配置實例)

    使用log4j2自定義配置文件位置和文件名(附log4j2.xml配置實例)

    這篇文章主要介紹了使用log4j2自定義配置文件位置和文件名(附log4j2.xml配置實例),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java基礎知識精通各種運算符

    Java基礎知識精通各種運算符

    計算機的最基本用途之一就是執(zhí)行數(shù)學運算,作為一門計算機語言,Java也提供了一套豐富的運算符來操縱變量,本篇對大家的學習或工作具有一定的價值,需要的朋友可以參考下
    2022-04-04
  • Java構造函數(shù)通透理解篇

    Java構造函數(shù)通透理解篇

    這篇文章主要介紹了Java構造函數(shù),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Java 是如何讀取和寫入瀏覽器Cookies的實例詳解

    Java 是如何讀取和寫入瀏覽器Cookies的實例詳解

    這篇文章主要介紹了Java 是如何讀取和寫入瀏覽器Cookies的實例的相關資料,需要的朋友可以參考下
    2016-09-09
  • 解決IDEA中不能正常輸入光標變粗的問題

    解決IDEA中不能正常輸入光標變粗的問題

    這篇文章主要介紹了在IDEA中不能正常輸入光標變粗的解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-09-09
  • springboot配置文件中使用${}注入值的兩種方式小結

    springboot配置文件中使用${}注入值的兩種方式小結

    這篇文章主要介紹了springboot配置文件中使用${}注入值的兩種方式小結,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot?SpringSecurity?JWT實現(xiàn)系統(tǒng)安全策略詳解

    SpringBoot?SpringSecurity?JWT實現(xiàn)系統(tǒng)安全策略詳解

    Spring?Security是Spring的一個核心項目,它是一個功能強大且高度可定制的認證和訪問控制框架。它提供了認證和授權功能以及抵御常見的攻擊,它已經成為保護基于spring的應用程序的事實標準
    2022-11-11
  • springboot cloud使用eureka整合分布式事務組件Seata 的方法

    springboot cloud使用eureka整合分布式事務組件Seata 的方法

    這篇文章主要介紹了springboot cloud使用eureka整合分布式事務組件Seata 的方法 ,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-05-05

最新評論

虞城县| 手游| 曲松县| 达州市| 郁南县| 开化县| 潼关县| 宝丰县| 塔城市| 庆安县| 鹿邑县| 隆回县| 望谟县| 本溪市| 筠连县| 惠水县| 大关县| 永顺县| 九寨沟县| 淮安市| 徐州市| 彰化县| 临西县| 宜州市| 天门市| 苏尼特右旗| 曲麻莱县| 壤塘县| 巢湖市| 天水市| 潮安县| 晋城| 平昌县| 保定市| 湖北省| 甘德县| 瓦房店市| 儋州市| 玉树县| 凤山市| 金湖县|