当前位置:网站首页>动态代理的实现原理
动态代理的实现原理
2022-06-29 22:46:00 【不败顽童博主】
动态代理
动态代理来源于设计模式中的代理模式,在这里“动态”的意思作者将其理解为更加灵活多变,在动态代理类中定义一个接口作(InvocationHandler)为成员变量,并且调用接口定义的方法(invoke),接口定义的方法(invoke)并不实现它,而是让这个接口的实现类(此处使用lambda表达式实现)来实现它。因此可以达到动态的效果。
Foo接口
interface Foo{
void foo();
void bar();
}
Foo接口的实现类Target
class Target implements Foo{
@Override
public void foo() {
System.out.println("foo()");
}
@Override
public void bar() {
System.out.println("bar()");
}
}
代理接口
// 代理接口
interface InvocationHandler{
void invoke(Method method,Object[] objects) throws Throwable;
}
代理类
public class $Proxy0 implements Foo {
/** * 使用InvocationHandler接口,将需要代理的方法的实现部分转移到其子类实现, * 而在代理类中只需要定一个接口,并在需要代理的方法中调用invoke方法即可。 */
private InvocationHandler h;
public $Proxy0(InvocationHandler h) {
this.h = h;
}
@Override
public void foo() {
try {
Method foo = Foo.class.getDeclaredMethod("foo");
h.invoke(foo,new Object[0]);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
@Override
public void bar() {
try {
Method bar = Foo.class.getDeclaredMethod("bar");
h.invoke(bar,new Object[0]);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
main方法类
public class Main{
public static void main(String[] args) {
$Proxy0 proxy = new $Proxy0(new InvocationHandler() {
@Override
public void invoke(Method method, Object[] objects) throws Throwable {
// 1、功能增强
System.out.println("before....");
// 2、原始方法
Object result = method.invoke(new Target(), objects);
}
});
proxy.foo();
proxy.bar();
}
}
边栏推荐
- Wireshark data analysis and forensics information pacapng
- The details of industry are all made by money and time
- Online text digit recognition list summation tool
- The third day
- 数据库-玩转数据-Pgsql 使用UUID做主键
- Is it safe to open an account on the flush? Where to apply for opening an account
- Evaluation of powerful and excellent document management software: image management, book management and document management
- 利用kubernetes中的leader选举机制来完成自己的HA应用
- 语音信号处理(二): 发声生理、听觉生理与听觉心理
- [learn FPGA programming from scratch -51]: high level chapter - FPGA development based on IP core - what is FPGA IP core (soft core, fixed core, hard core) and learning methods
猜你喜欢

NRM explanation

云原生爱好者周刊:炫酷的 Grafana 监控面板集合

剑指 Offer 38. 字符串的排列

Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first

语音信号处理(三):语音信号分析【连续的“模拟信号”--采样、量化、编码-->离散的“数字信号”】
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet

Summer rainbow comes for dinner
![[php8+oracle11g+windows environment without tools] Intranet / no network /win10/php connecting to Oracle database instance](/img/72/214ee6d3842f393164cc93bb387926.png)
[php8+oracle11g+windows environment without tools] Intranet / no network /win10/php connecting to Oracle database instance

With the rise of China's database, Alibaba cloud lifeifei: China's cloud database has taken the lead in various mainstream technological innovations abroad

Touch key and key control corresponding LED status reversal
随机推荐
R & D test time ratio, bug data analysis
mysql备份数据库linux
How tcpdump filters specific TCP flag bits
Summer rainbow comes for dinner
LeetCode85+105+114+124
Talk about auto in MySQL in detail_ What is the function of increment
constexpr 函数
leetcode 416. Partition Equal Subset Sum 分割等和子集(中等)
uniapp复制内容到剪贴板
JS function related review
How ZABBIX 5.0 adds esxi6.7 to monitoring
sql刷题595. 大的国家
详细聊聊MySQL中auto_increment有什么作用
wirehark数据分析与取证infiltration.pacapng
Kr-gcn: an interpretable recommendation system based on knowledge aware reasoning
80-Redis详解
在线文本数字识别列表求和工具
Wireshark data analysis and forensics information pacapng
How to use filters in jfinal to monitor Druid for SQL execution?
Deep parsing of kubernetes controller runtime