Proxy动态代理
Proxy动态代理
1 package com.test.dynamicproxy; 2 3 public interface Subject { 4 5 public void request(); 6 }
1 package com.test.dynamicproxy; 2 3 public class RealSubject implements Subject { 4 5 @Override 6 public void request() { 7 System.out.println("request() from RealSubject"); 8 9 } 10 11 }
1 package com.test.dynamicproxy; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Method; 5 6 public class SubjectInvocationHandler implements InvocationHandler { 7 8 private Object object; 9 10 public SubjectInvocationHandler(Object obj) { 11 this.object = obj; 12 } 13 14 @Override 15 public Object invoke(Object proxy, Method method, Object[] args) 16 throws Throwable { 17 method.invoke(object,args); 18 return null; 19 } 20 21 }
1 package com.test.dynamicproxy; 2 3 import java.lang.reflect.InvocationHandler; 4 import java.lang.reflect.Proxy; 5 6 public class Client { 7 8 public static void main(String[] args) { 9 10 RealSubject real = new RealSubject(); 11 InvocationHandler ih = new SubjectInvocationHandler(real); 12 Class<?> clazz = real.getClass(); 13 Subject s = (Subject)Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),ih); 14 15 s.request(); //request() from RealSubject 16 17 System.out.println(s.getClass().getName()); //$Proxy0 18 System.out.println(s instanceof Proxy); //true 19 20 for(Class interf : s.getClass().getInterfaces()){ 21 System.out.println(interf.getName()); //com.test.dynamicproxy.Subject 22 } 23 24 25 // System.out.println(s.getClass().getPackage()); //null 26 // System.out.println(s.getClass().getSuperclass()); //class java.lang.reflect.Proxy 27 // System.out.println(Proxy.class.getPackage()); //package java.lang.reflect, Java Platform API Specification, version 1.6 28 } 29 30 }
Object java.lang.reflect.Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler. This method is equivalent to:
Proxy.getProxyClass(loader, interfaces). getConstructor(new Class[] { InvocationHandler.class }). newInstance(new Object[] { handler });
Proxy.newProxyInstance
throws IllegalArgumentException
for the same reasons that Proxy.getProxyClass
does.
- Parameters:
- loader the class loader to define the proxy class
- interfaces the list of interfaces for the proxy class to implement
- h the invocation handler to dispatch method invocations to
- Returns:
- a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces
- Throws:
- IllegalArgumentException - if any of the restrictions on the parameters that may be passed to
getProxyClass
are violated - NullPointerException - if the
interfaces
array argument or any of its elements arenull
, or
Client 中 s extends Proxy implements Subject
s.request() 方法调用流程 :
s 是 Subject 的 implementation .s.request() 的实现
s 伪代码:
s extends Proxy implements Subject{
InvocationHandler ih;
......
public void request(){
ih.invoke(....);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决