一直不明白 InvocationHandlerinvoke() 中 proxy 参数有什么作用?

一直不明白 InvocationHandlerinvoke() 中 proxy 参数有什么作用?故事开始一直不明白 Java InvocationHa 类的 invoke 方法中 proxy 参数有什么作用呢

大家好,欢迎来到IT知识分享网。

故事开始

一直不明白 Java InvocationHandler 类的 invoke() 方法中 proxy 参数有什么作用呢?

先来看一段代码,如下所示:

public class DynamicProxyHandler implements InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // ...doSomething... } }

疑问:有人知道,在 invoke() 方法中的第一个入参 proxy 有什么作用嘛?一直都没搞懂这个参数有啥用,今天我就要发掘出属于它的其中一个作用。让我们开始吧!

接下来我们将创建一个动态代理,它不仅记录方法调用,还能根据代理对象的不同执行不同的逻辑,代码如下。

步骤 1: 定义接口[玫瑰]

首先,我们定义两个接口,每个接口包含一个方法。

public interface GreetingService { void sayHello(String name); } public interface FarewellService { void sayGoodbye(String name); }

步骤 2: 实现 InvocationHandler[玫瑰]

接下来,我们创建一个 InvocationHandler 实现,它将根据代理对象的不同执行不同的逻辑。

import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class DynamicProxyHandler implements InvocationHandler { private final Object target; public DynamicProxyHandler(Object target) { this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // 根据代理对象的类型执行不同的逻辑 if (proxy instanceof GreetingService) { System.out.println("GreetingService proxy used"); } else if (proxy instanceof FarewellService) { System.out.println("FarewellService proxy used"); } System.out.println("Method " + method.getName() + " is called with arguments " + args); // 调用实际对象的方法 Object result = method.invoke(target, args); System.out.println("Method " + method.getName() + " returned " + result); return result; } }

步骤 3: 创建代理对象[玫瑰]

现在,我们创建两个代理对象,一个用于 GreetingService,另一个用于 FarewellService

public class ProxyDemo { public static void main(String[] args) { // 实际对象 GreetingService realGreetingService = (name) -> System.out.println("Hello " + name); FarewellService realFarewellService = (name) -> System.out.println("Goodbye " + name); // 创建 GreetingService 代理 GreetingService proxyGreetingService = (GreetingService) Proxy.newProxyInstance( GreetingService.class.getClassLoader(), new Class<?>[]{GreetingService.class}, new DynamicProxyHandler(realGreetingService) ); // 创建 FarewellService 代理 FarewellService proxyFarewellService = (FarewellService) Proxy.newProxyInstance( FarewellService.class.getClassLoader(), new Class<?>[]{FarewellService.class}, new DynamicProxyHandler(realFarewellService) ); // 通过代理调用方法 proxyGreetingService.sayHello("Kimi"); proxyFarewellService.sayGoodbye("Kimi"); } }

程序输出[玫瑰]

当你运行 ProxyDemo 类时,输出将如下所示:

GreetingService proxy used Method sayHello is called with arguments [Kimi] Hello Kimi Method sayHello returned null FarewellService proxy used Method sayGoodbye is called with arguments [Kimi] Goodbye Kimi Method sayGoodbye returned null

最后来个小总结[玫瑰]

在我们的上述这个例子中:

  • GreetingServiceFarewellService 是两个不同的接口。
  • DynamicProxyHandlerInvocationHandler 的实现,它根据代理对象的类型执行不同的逻辑。
  • ProxyDemomain 方法中,我们创建了两个不同的代理对象,一个用于 GreetingService,另一个用于 FarewellService
  • 我们使用 Proxy.newProxyInstance 方法创建了两个代理对象,它们都使用同一个 DynamicProxyHandler 实例,但是 DynamicProxyHandler 能够根据 proxy 参数的不同区分代理对象的类型。
  • 当我们通过代理对象调用方法时,DynamicProxyHandlerinvoke 方法被调用,它根据 proxy 参数的类型记录了不同的信息,并执行了相应的方法调用。

展示了 proxy 参数如何用于区分不同的代理对象,并允许 InvocationHandler 根据代理对象执行特定的逻辑。通过检查 proxy 参数的类型,DynamicProxyHandler 能够决定如何响应方法调用,从而实现了基于代理对象类型的动态行为。

那么,最后,你还发现 proxy 入参还有哪些用法呢?可以Q在评论区哦!

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/152608.html

(0)
上一篇 2025-03-08 14:26
下一篇 2025-03-08 14:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信