当前位置:网站首页>動態代理的實現原理
動態代理的實現原理
2022-06-29 23:13: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();
}
}
边栏推荐
- 缓冲流练习
- 基金的估值,费用,会计核算
- 云原生爱好者周刊:炫酷的 Grafana 监控面板集合
- What if MySQL fails to store emoticons
- 正则表达式:字符(2)
- 把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
- Laravel 创建自己的 Facade 扩展 geoip 根据 IP 获取国家、地域、城市信息
- 字节云数据库未来方向的探索与实践
- 成为唯一的key
- How to solve the problem that the computer time is not automatically updated after proofreading
猜你喜欢

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

疫情下我离职一年,收入增长了10倍

Cloud native enthusiast weekly: cool collection of grafana monitoring panels

AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs

Qt中使用QDomDocument和QDomnode来读取xml

Still stay up late every day and work overtime to make statements? In fact, you don't know how to make reports efficiently

constexpr 函数

math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)

什么是IGMP?IGMP与ICMP有啥区别?

深入解析kubernetes controller-runtime
随机推荐
Hematemesis finishing: a rare map of architects!
Does Australia require that PVC plastic sheets comply with as/nzs 1530.3 with a flame spread index of 0?
Phpspreadsheet reading and writing Excel files
Become the only key
基金的信息披露
How to use filters in jfinal to monitor Druid for SQL execution?
Laravel 创建自己的 Facade 扩展 geoip 根据 IP 获取国家、地域、城市信息
Unity Pac Man games, maze implementation
Day9 - user registration and login
The soft youth under the blessing of devcloud makes education "smart" in the cloud
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
Realizing deep learning framework from zero -- RNN from theory to practice [practice]
Hidden worries behind the listing of shushulang: the performance has declined significantly, the market position is relatively backward, and the competitiveness is questionable
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
深入解析kubernetes controller-runtime
优雅的改造短信业务模块,策略模式走起!
Qt5.14.2 error connecting to the MySQL database of Ubuntu 20.04
2022 PMP project management examination agile knowledge points (5)
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
Polymorphism of laravel association model