Java反射的應用之動態(tài)代理深入理解
更新時間:2021年09月13日 10:21:58 作者:威斯布魯克.猩猩
這篇文章主要介紹了Java反射的應用之動態(tài)代理深入理解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、代理模式的引入



靜態(tài)代理舉例
特點:代理類和被代理類在編譯期間,就確定下來了。
interface ClothFactory{
void produceCloth();
}
//代理類
class ProxyClothFactory implements ClothFactory{
private ClothFactory factory;//用被代理類對象進行實例化
public ProxyClothFactory(ClothFactory factory){
this.factory = factory;
}
@Override
public void produceCloth() {
System.out.println("代理工廠做一些準備工作");
factory.produceCloth();
System.out.println("代理工廠做一些后續(xù)的收尾工作");
}
}
//被代理類
class NikeClothFactory implements ClothFactory{
@Override
public void produceCloth() {
System.out.println("Nike工廠生產(chǎn)一批運動服");
}
}
public class StaticProxyTest {
public static void main(String[] args) {
//創(chuàng)建被代理類的對象
NikeClothFactory nike = new NikeClothFactory();
//創(chuàng)建代理類的對象
ProxyClothFactory proxyClothFactory = new ProxyClothFactory(nike);
proxyClothFactory.produceCloth();
}
二、動態(tài)代理



動態(tài)代理的舉例
interface Human{
String getBelief();
void eat(String food);
}
//被代理類
class SuperMan implements Human{
@Override
public String getBelief() {
return "I believe I can fly!";
}
@Override
public void eat(String food) {
System.out.println("我喜歡吃" + food);
}
}
class HumanUtil{
public void method1(){
System.out.println("===================通用方法一===============================");
}
public void method2(){
System.out.println("===================通用方法二===============================");
}
}
/*
要想實現(xiàn)動態(tài)代理,需要解決的問題?
問題一:如何根據(jù)加載到內(nèi)存中的被代理類,動態(tài)的創(chuàng)建一個代理類及其對象。
問題二:當通過代理類的對象調(diào)用方法a時,如何動態(tài)的去調(diào)用被代理類中的同名方法a。
*/
class ProxyFactory{
//調(diào)用此方法,返回一個代理類的對象。解決問題一
public static Object getProxyInstance(Object obj){//obj:被代理類的對象
MyInvocationHandler handler = new MyInvocationHandler();
handler.bind(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),handler);
}
}
class MyInvocationHandler implements InvocationHandler{
private Object obj;//需要使用被代理類的對象進行賦值
public void bind(Object obj){
this.obj = obj;
}
//當我們通過代理類的對象,調(diào)用方法a時,就會自動的調(diào)用如下的方法:invoke()
//將被代理類要執(zhí)行的方法a的功能就聲明在invoke()中
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
HumanUtil util = new HumanUtil();
util.method1();
//method:即為代理類對象調(diào)用的方法,此方法也就作為了被代理類對象要調(diào)用的方法
//obj:被代理類的對象
Object returnValue = method.invoke(obj,args);
util.method2();
//上述方法的返回值就作為當前類中的invoke()的返回值。
return returnValue;
}
}
public class ProxyTest{
public static void main(String[] args) {
SuperMan superMan = new SuperMan();
Human proxyInstance = (Human) ProxyFactory.getProxyInstance(superMan);
//當通過代理類對象調(diào)用方法時,會自動的調(diào)用被代理類中同名的方法
String belief = proxyInstance.getBelief();
System.out.println(belief);
proxyInstance.eat("四川麻辣燙");
System.out.println("************************************************");
NikeClothFactory nikeClothFactory = new NikeClothFactory();
ClothFactory proxyClothFactory = (ClothFactory) ProxyFactory.getProxyInstance(nikeClothFactory);
proxyClothFactory.produceCloth();
}
到此這篇關(guān)于Java反射的應用之動態(tài)代理深入理解的文章就介紹到這了,更多相關(guān)Java動態(tài)代理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot對靜態(tài)資源的映射規(guī)則詳解解讀
這篇文章主要介紹了SpringBoot對靜態(tài)資源的映射規(guī)則詳解解讀,在Spring Boot中,映射規(guī)則是用來定義URL與控制器方法之間的映射關(guān)系的,通過映射規(guī)則,可以將特定的URL請求映射到相應的控制器方法上,從而實現(xiàn)請求的處理和響應的返回,需要的朋友可以參考下2023-10-10
如何在SpringBoot中添加攔截器忽略請求URL當中的指定字符串
這篇文章主要介紹了在SpringBoot中添加攔截器忽略請求URL當中的指定字符串,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
SpringBoot如何實現(xiàn)并發(fā)任務并返回結(jié)果
這篇文章主要介紹了SpringBoot如何實現(xiàn)并發(fā)任務并返回結(jié)果問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot+MyBatis-Plus實現(xiàn)分頁的項目實踐
MyBatis-Plus是基于MyBatis的持久層增強工具,提供簡化CRUD、代碼生成器、條件構(gòu)造器、分頁及樂觀鎖等功能,極大簡化了開發(fā)工作量并提高了開發(fā)效率,本文就來介紹一下SpringBoot+MyBatis-Plus實現(xiàn)分頁的項目實踐,感興趣的可以了解一下2024-11-11

