Java動(dòng)態(tài)代理和AOP應(yīng)用示例
本文實(shí)例講述了Java動(dòng)態(tài)代理和AOP應(yīng)用。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
動(dòng)態(tài)代理在AOP(Aspect Orient Program,即面向切面編程)里被稱為AOP代理,AOP代理可代替目標(biāo)對(duì)象,AOP代理包含了目標(biāo)對(duì)象的全部方法。但AOP代理中的方法與目標(biāo)對(duì)象的方法存在差異:AOP代理里的方法可以在執(zhí)行目標(biāo)方法之前、之后插入一些通用處理。

二 代碼
Dog.java
public interface Dog
{
// info方法聲明
void info();
// run方法聲明
void run();
}
GunDog.java
public class GunDog implements Dog
{
// 實(shí)現(xiàn)info()方法,僅僅打印一個(gè)字符串
public void info()
{
System.out.println("我是一只獵狗");
}
// 實(shí)現(xiàn)run()方法,僅僅打印一個(gè)字符串
public void run()
{
System.out.println("我奔跑迅速");
}
}
DogUtil.java
public class DogUtil
{
// 第一個(gè)攔截器方法
public void method1()
{
System.out.println("=====模擬第一個(gè)通用方法=====");
}
// 第二個(gè)攔截器方法
public void method2()
{
System.out.println("=====模擬通用方法二=====");
}
}
MyInvokationHandler.java
import java.lang.reflect.*;
public class MyInvokationHandler implements InvocationHandler
{
// 需要被代理的對(duì)象
private Object target;
public void setTarget(Object target)
{
this.target = target;
}
// 執(zhí)行動(dòng)態(tài)代理對(duì)象的所有方法時(shí),都會(huì)被替換成執(zhí)行如下的invoke方法
public Object invoke(Object proxy, Method method, Object[] args)
throws Exception
{
DogUtil du = new DogUtil();
// 執(zhí)行DogUtil對(duì)象中的method1。
du.method1();
// 以target作為主調(diào)來(lái)執(zhí)行method方法
Object result = method.invoke(target , args);
// 執(zhí)行DogUtil對(duì)象中的method2。
du.method2();
return result;
}
}
MyProxyFactory.java
import java.lang.reflect.*;
public class MyProxyFactory
{
// 為指定target生成動(dòng)態(tài)代理對(duì)象
public static Object getProxy(Object target)
throws Exception
{
// 創(chuàng)建一個(gè)MyInvokationHandler對(duì)象
MyInvokationHandler handler =
new MyInvokationHandler();
// 為MyInvokationHandler設(shè)置target對(duì)象
handler.setTarget(target);
// 創(chuàng)建、并返回一個(gè)動(dòng)態(tài)代理
return Proxy.newProxyInstance(target.getClass().getClassLoader()
, target.getClass().getInterfaces() , handler);
}
}
Test.java
public class Test
{
public static void main(String[] args)
throws Exception
{
// 創(chuàng)建一個(gè)原始的GunDog對(duì)象,作為target
Dog target = new GunDog();
// 以指定的target來(lái)創(chuàng)建動(dòng)態(tài)代理
Dog dog = (Dog)MyProxyFactory.getProxy(target);
dog.info();
dog.run();
}
}
三 運(yùn)行
E:\Java\瘋狂java講義\codes\18\18.5\DynaProxy>java Test
=====模擬第一個(gè)通用方法=====
我是一只獵狗
=====模擬通用方法二=====
=====模擬第一個(gè)通用方法=====
我奔跑迅速
=====模擬通用方法二=====
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Springboot并發(fā)調(diào)優(yōu)之大事務(wù)和長(zhǎng)連接
這篇文章主要介紹了Springboot并發(fā)調(diào)優(yōu)之大事務(wù)和長(zhǎng)連接,重點(diǎn)分享長(zhǎng)事務(wù)以及長(zhǎng)連接導(dǎo)致的并發(fā)排查和優(yōu)化思路和示例,具有一定的參考價(jià)值,感興趣的可以了解一下2022-05-05
mybatis如何使用Java8的日期LocalDate和LocalDateTime詳解
這篇文章主要給大家介紹了關(guān)于mybatis如何使用Java8的日期LocalDate和LocalDateTime的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
如何基于Java實(shí)現(xiàn)對(duì)象List排序
這篇文章主要介紹了如何基于Java實(shí)現(xiàn)對(duì)象List排序,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
springboot如何使用thymeleaf完成頁(yè)面緩存
這篇文章主要介紹了springboot如何使用thymeleaf完成頁(yè)面緩存,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Spring security如何實(shí)現(xiàn)記錄用戶登錄時(shí)間功能
這篇文章主要介紹了Spring security如何實(shí)現(xiàn)記錄用戶登錄時(shí)間功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

