Java中反射動(dòng)態(tài)代理接口的詳解及實(shí)例
Java語(yǔ)言中反射動(dòng)態(tài)代理接口的解釋與演示
Java在JDK1.3的時(shí)候引入了動(dòng)態(tài)代理機(jī)制、可以運(yùn)用在框架編程與平臺(tái)編程時(shí)候捕獲事件、審核數(shù)據(jù)、日志等功能實(shí)現(xiàn),首先看一下設(shè)計(jì)模式的UML圖解:
當(dāng)你調(diào)用一個(gè)接口API時(shí)候,實(shí)際實(shí)現(xiàn)類繼承該接口,調(diào)用時(shí)候經(jīng)過(guò)proxy實(shí)現(xiàn)。
在Java中動(dòng)態(tài)代理實(shí)現(xiàn)的兩個(gè)關(guān)鍵接口類與class類分別如下:
java.lang.reflect.Proxy
java.lang.reflect.InvocationHandler
我們下面就通過(guò)InvocationHandler接口來(lái)實(shí)現(xiàn)動(dòng)態(tài)代理過(guò)程,通過(guò)Proxy接口創(chuàng)建
一個(gè)代理類,然后測(cè)試完整的程序。要實(shí)現(xiàn)演示Demo需要如下幾步:
一:首先定義我們自己的POJO對(duì)象接口類IExample與IUser
package com.example.pojo; public interface IExample { public void setName(String name); public String getName(); public void setDesc(String description); public String getDesc(); }
package com.example.pojo; public interface IUser { public void setUserID(String userID); public String getUserID(); public void setUserName(String userName); public String getUserName(); }
二:實(shí)現(xiàn)我們自己InvocationHandler接口,其中map我用來(lái)存儲(chǔ)POJO對(duì)象的數(shù)據(jù),這樣做的好處是POJO接口無(wú)需再創(chuàng)建實(shí)現(xiàn)類,只有定義接口就可以通過(guò)代理直接使用該類,這在實(shí)際項(xiàng)目開(kāi)發(fā)中非常有用。
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; } }
三:通過(guò)Proxy方法初始化代理得到POJO對(duì)象,運(yùn)行與測(cè)試:
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()); } }
四:運(yùn)行結(jié)果如下:
exmaple name : my example exmaple desc : my proxy example user ID : jia20003 user name : gloomyfish
Java動(dòng)態(tài)代理方式對(duì)框架編程非常重要無(wú)論是在Web端還是桌面端
而真正把這種技術(shù)發(fā)揚(yáng)光大的則是spring框架。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java開(kāi)發(fā)完整短信驗(yàn)證碼功能的全過(guò)程
利用短信驗(yàn)證碼進(jìn)行身份驗(yàn)證是目前互聯(lián)網(wǎng)眾多產(chǎn)品常用的一種方式,那么這種短信驗(yàn)證功能是如何實(shí)現(xiàn)的呢,下面這篇文章主要給大家介紹了關(guān)于Java開(kāi)發(fā)完整短信驗(yàn)證碼功能的相關(guān)資料,需要的朋友可以參考下2021-10-10如何在Java中使用標(biāo)準(zhǔn)庫(kù)創(chuàng)建臨時(shí)文件
有時(shí)候我們程序運(yùn)行時(shí)需要產(chǎn)生中間文件,但是這些文件只是臨時(shí)用途,并不做長(zhǎng)久保存,我們可以使用臨時(shí)文件,不需要長(zhǎng)久保存,這篇文章主要給大家介紹了關(guān)于如何在Java中使用標(biāo)準(zhǔn)庫(kù)創(chuàng)建臨時(shí)文件的相關(guān)資料,需要的朋友可以參考下2023-10-10SpringBoot整合Shiro的環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了SpringBoot整合Shiro的環(huán)境搭建教程,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以了解一下2022-12-12java設(shè)計(jì)簡(jiǎn)單學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)簡(jiǎn)單學(xué)生管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09