Java JDK * 实现原理实例解析
作者:夏的世界的伤 时间:2022-04-23 05:19:10
JDK * 实现原理
* 机制
通过实现 InvocationHandler 接口创建自己的调用处理器
通过为 Proxy 类指定 ClassLoader 对象和一组 interface 来创建 * 类
通过反射机制获得 * 类的构造函数,其唯一参数类型是调用处理器接口类型
通过构造函数创建 * 类实例,构造时调用处理器对象作为参数被传入
Interface InvocationHandler
该接口中仅定义了一个方法Object:invoke(Object obj,Method method,Object[] args)。在实际使用时,第一个参数obj一般是指代理类,method是被代理的方法,args为该方法的参数数组。这个抽象方法在代理类中动态实现。
Proxy
该类即为 * 类
Protected Proxy(InvocationHandler h)
构造函数,用于给内部的h赋值
Static Class getProxyClass (ClassLoader loader,Class[] interfaces)
获得一个代理类,其中loader是类装载器,interfaces是真实类所拥有的全部接口的数组
Static Object newProxyInstance(ClassLoader loader,Class[] interfaces,InvocationHandler h)
返回代理类的一个实例,返回后的代理类可以当作被代理类使用(可使用被代理类的在Subject接口中声明过的方法)
Dynamic Proxy
它是在运行时生成的class,在生成它时你必须提供一组interface给它,然后该class就宣称它实现了这些 interface。你当然可以把该class的实例当作这些interface中的任何一个来用。当然啦,这个Dynamic Proxy其实就是一个Proxy,它不会替你作实质性的工作,在生成它的实例时你必须提供一个handler,由它接管实际的工作。
代码示例
创建接口:
/**
* @CreateDate: 2019/6/17 14:52
* @Version: 1.0
*/
public interface BuyService {
String buyPhone();
String buyComputer();
}
创建实现类:
public class BuyServiceImpl implements BuyService {
@Intercept("buyPhone")
@Override
public String buyPhone() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("==========BuyServiceImpl.class=============" + " buyPhone");
this.buyComputer();
return "buy phone";
}
@Intercept("buyComputer")
@Override
public String buyComputer() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("==========BuyServiceImpl.class=============" + " buyComputer");
return "buy computer";
}
}
创建 InvocationHandler:
public class ReflectionHandler implements InvocationHandler {
private Object target;
public ReflectionHandler(Object target) {
this.target = target;
}
public <T> T getProxy(){
return (T) Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(target,args);
}
}
创建启动类:
public class Bootstrap {
public static void main(String[] args) {
// * 实现
ReflectionHandler reflectionHandler = new ReflectionHandler(new BuyServiceImpl());
BuyService proxy = reflectionHandler.getProxy();
String computer = proxy.buyComputer();
String phone = proxy.buyPhone();
System.out.println(computer + "\r\n" + phone);
}
来源:https://www.cnblogs.com/hkMblogs/p/13171733.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java开发ShardingSphere的路由引擎类型示例详解
使用@TransactionalEventListener监听事务教程
浅谈Java中复制数组的方式
新手入门Jvm-- JVM对象创建与内存分配机制
![](https://img.aspxhome.com/file/2023/4/66704_0s.png)
基于sharding-jdbc的使用限制
Lombok中@EqualsAndHashCode注解的使用及说明
![](https://img.aspxhome.com/file/2023/4/64204_0s.png)
C# 获取当前年份的周期及周期所在日期范围(推荐)
![](https://img.aspxhome.com/file/2023/1/66131_0s.png)
线程池中execute与submit的区别说明
![](https://img.aspxhome.com/file/2023/6/64786_0s.png)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.Transformer异常
用Flutter开发自定义Plugin的方法示例
![](https://img.aspxhome.com/file/2023/3/99793_0s.png)
C++找出字符串中出现最多的字符和次数,时间复杂度小于O(n^2)
SpringBoot集成阿里云OSS图片上传
![](https://img.aspxhome.com/file/2023/9/66979_0s.jpg)
详解Servlet3.0新特性(从注解配置到websocket编程)
![](https://img.aspxhome.com/file/2023/3/58033_0s.png)
Java中的synchronized关键字
Map与JavaBean相互转换的工具类
Spring中的@Transactional的工作原理
![](https://img.aspxhome.com/file/2023/8/58028_0s.webp)
Spring JPA联表查询之OneToOne源码详解
![](https://img.aspxhome.com/file/2023/3/67603_0s.jpg)
基于java构造方法Vector删除元素源码分析
![](https://img.aspxhome.com/file/2023/8/60238_0s.png)
Java Apache common-pool对象池介绍
![](https://img.aspxhome.com/file/2023/8/63328_0s.png)
Java使用Lettuce客户端在Redis在主从复制模式下命令执行的操作
![](https://img.aspxhome.com/file/2023/7/60797_0s.jpg)