案例:
1
2
3
4
|
public interface forumservice { void removetopic( int topicid); void removeforum( int forumid); } |
對相關方法進行性能監控
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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(); } } |
性能監控實現類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class performancemonitor { // 通過一個threadlocal保存與調用線程相關的性能監視信息 private static threadlocal<methodperformance> performancerecord = new threadlocal<methodperformance>(); // 啟動對某一目標方法的性能監視 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(); // 打印出方法性能監視的結果信息 mp.printperformance(); } } |
用于記錄性能監控信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class performancemonitor { // 通過一個threadlocal保存與調用線程相關的性能監視信息 private static threadlocal<methodperformance> performancerecord = new threadlocal<methodperformance>(); // 啟動對某一目標方法的性能監視 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(); // 打印出方法性能監視的結果信息 mp.printperformance(); } } |
1、jdk動態代理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class performancemonitor { // 通過一個threadlocal保存與調用線程相關的性能監視信息 private static threadlocal<methodperformance> performancerecord = new threadlocal<methodperformance>(); // 啟動對某一目標方法的性能監視 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(); // 打印出方法性能監視的結果信息 mp.printperformance(); } } |
1
2
3
4
5
6
7
8
9
10
11
|
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動態代理
1
2
3
4
5
6
|
<!-- https: //mvnrepository.com/artifact/cglib/cglib --> <dependency> <groupid>cglib</groupid> <artifactid>cglib</artifactid> <version> 2.2 . 2 </version> </dependency> |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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; } } |
1
2
3
4
5
6
7
8
9
|
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的區別
- jdk動態代理只能對實現了接口的類生成代理,而不能針對類
- cglib是針對類實現代理,主要是對指定的類生成一個子類,覆蓋其中的方法(繼承)
2)、spring在選擇用jdk還是cglib的依據
- 當bean實現接口時,spring就會用jdk的動態代理
- 當bean沒有實現接口時,spring使用cglib來實現
- 可以強制使用cglib(在spring配置中加入<aop:aspectj-autoproxy proxy-target-class=“true”/>)
3)、jdk和cglib的性能對比
- 使用cglib實現動態代理,cglib底層采用asm字節碼生成框架,使用字節碼技術生成代理類,在jdk1.6之前比使用java反射效率要高。唯一需要注意的是,cglib不能對聲明為final的方法進行代理,因為cglib原理是動態生成被代理類的子類。
- 在jdk1.6、jdk1.7、jdk1.8逐步對jdk動態代理優化之后,在調用次數較少的情況下,jdk代理效率高于cglib代理效率,只有當進行大量調用的時候,jdk1.6和jdk1.7比cglib代理效率低一點,但是到jdk1.8的時候,jdk代理效率高于cglib代理
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/qq_40378034/article/details/86504177