Java在JDK1.3的時候引入了動態代理機制、可以運用在框架編程與平臺編程時候捕獲事件、審核數據、日志等功能實現,首先看一下設計模式的UML圖解:
當你調用一個接口API時候,實際實現類繼承該接口,調用時候經過proxy實現。
在Java中動態代理實現的兩個關鍵接口類與class類分別如下:
java.lang.reflect.Proxy
java.lang.reflect.InvocationHandler
我們下面就通過InvocationHandler接口來實現動態代理過程,通過Proxy接口創建
一個代理類,然后測試完整的程序。要實現演示Demo需要如下幾步:
一:首先定義我們自己的POJO對象接口類IExample與IUser
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.example.pojo; public interface IExample { public void setName(String name); public String getName(); public void setDesc(String description); public String getDesc(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.example.pojo; public interface IUser { public void setUserID(String userID); public String getUserID(); public void setUserName(String userName); public String getUserName(); } |
二:實現我們自己InvocationHandler接口,其中map我用來存儲POJO對象的數據,這樣做的好處是POJO接口無需再創建實現類,只有定義接口就可以通過代理直接使用該類,這在實際項目開發中非常有用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package com.example.reflection; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.Map; public class MyProxyView implements InvocationHandler { private Map<Object, Object> map = null ; public static Object newInstance(Class[] interfaces) { return Proxy.newProxyInstance(MyProxyView. class .getClassLoader(), interfaces, new MyProxyView()); } private MyProxyView() { this .map = new HashMap<Object, Object>(); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = null ; String methodName = method.getName(); if (methodName.startsWith( "get" )) { String name = methodName.substring(methodName.indexOf( "get" ) + 3 ); return map.get(name); } else if (methodName.startsWith( "set" )) { String name = methodName.substring(methodName.indexOf( "set" ) + 3 ); map.put(name, args[ 0 ]); return null ; } else if (methodName.startsWith( "is" )) { String name = methodName.substring(methodName.indexOf( "is" ) + 2 ); return (map.get(name)); } return result; } } |
三:通過Proxy方法初始化代理得到POJO對象,運行與測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.example.reflection; import com.example.pojo.IExample; import com.example.pojo.IUser; public class TextProxy { public static void main(String[] args) { IExample example = (IExample)MyProxyView.newInstance( new Class[]{IExample. class }); IUser user = (IUser)MyProxyView.newInstance( new Class[]{IUser. class }); // aduit bean 1 example.setName( "my example" ); example.setDesc( "my proxy example" ); // aduit bean 2 user.setUserID( "jia20003" ); user.setUserName( "gloomyfish" ); System.out.println( "exmaple name : " + example.getName()); System.out.println( "exmaple desc : " + example.getDesc()); System.out.println(); System.out.println( "user ID : " + user.getUserID()); System.out.println( "user name : " + user.getUserName()); } } |
四:運行結果如下:
1
2
3
4
5
|
exmaple name : my example exmaple desc : my proxy example user ID : jia20003 user name : gloomyfish |
Java動態代理方式對框架編程非常重要無論是在Web端還是桌面端
而真正把這種技術發揚光大的則是spring框架。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/jia20003/article/details/49930043