Java知识分享网 - 轻松学习从此开始!    

Java知识分享网

Java1234官方群25:java1234官方群17
Java1234官方群25:838462530
        
SpringBoot+SpringSecurity+Vue+ElementPlus权限系统实战课程 震撼发布        

最新Java全栈就业实战课程(免费)

springcloud分布式电商秒杀实战课程

IDEA永久激活

66套java实战课程无套路领取

锋哥开始收Java学员啦!

Python学习路线图

锋哥开始收Java学员啦!
当前位置: 主页 > Java文档 > Java基础相关 >

Java 动态代理Proxy应用和底层源码分析 PDF 下载


分享到:
时间:2024-04-24 10:16来源:http://www.java1234.com 作者:转载  侵权举报
Java 动态代理Proxy应用和底层源码分析
失效链接处理
Java 动态代理Proxy应用和底层源码分析 PDF 下载
 
 
 
相关截图:
 

主要内容:

前言
Java Proxy主要用于创建动态代理实例,这些实例实现了指定的一组接口,并在调用方法时将调用转发
给指定的调用处理器(InvocationHandler)。这种机制常用于实现AOP(面向切面编程)框架、RPC
(远程过程调用)框架等,以及任何需要动态改变对象行为的场景。
 
一、Proxy代码示例
在这个示例中,我们创建了一个实现了 MyInterface 接口的匿名内部类对象 myObject。然后,我们创
建了一个 MyInvocationHandler 对象,并将 myObject 作为目标对象传递给它。接下来,我们使用
Proxy.newProxyInstance 方法创建了一个代理对象 proxy,并将 MyInterface 接口和 handler 作为参
数传递给它。最后,我们调用代理对象的 doSomething 方法,这个方法调用会转发给 handler 
invoke 方法,并在其中添加自定义逻辑。
 
interface MyInterface {
void doSomething();
}
class MyInvocationHandler implements InvocationHandler {
private Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws
Throwable {
System.out.println("Before method call");
Object result = method.invoke(target, args);
System.out.println("After method call");
return result;
}
}
public class DynamicProxyExample {
public static void main(String[] args) {
MyInterface myObject = new MyInterface() {
@Override
public void doSomething() {
System.out.println("Doing something");
}
};
MyInvocationHandler handler = new MyInvocationHandler(myObject);
MyInterface proxy = (MyInterface) Proxy.newProxyInstance(
MyInterface.class.getClassLoader(),
new Class<?>[]{MyInterface.class},
handler
);
proxy.doSomething(); // This will call the proxy's doSomething method,
which forwards to the handler's invoke method.
}
}

 

二、底层源码分析
newProxyInstance这个入口来看看Proxy的重点方法。
 
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h) throws
IllegalArgumentException {
//将接口clone,之后对此clone类进行操作
final Class<?>[] intfs = interfaces.clone();
//进行权限检查
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
}
/*
* Look up or generate the designated proxy class.
* 根据传入的类加载器和接口类数组,生成相应的代理类
*/
Class<?> cl = getProxyClass0(loader, intfs);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (sm != null) {
checkNewProxyPermission(Reflection.getCallerClass(), cl);
}
//获取构造
final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doexposingd(new PrivilegedAction<Void>() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
//返回代理对象
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
}
}

 

 

 
------分隔线----------------------------

锋哥公众号


锋哥微信


关注公众号
【Java资料站】
回复 666
获取 
66套java
从菜鸡到大神
项目实战课程

锋哥推荐