Java 几种动态代理实现及其性能比较
原处出之于阿里liangf
Interface:
package com.sunchao.jdkdyproxy; public interface Subject { void request();
}
impl:
package com.sunchao.jdkdyproxy; public class RealSubject implements Subject {
private int count;
@Override
public void request() {
count++;
//System.out.println("real subject does with the request!");
} }
jdk:
package com.sunchao.jdkdyproxy; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy; public class ClientProxy { public static Subject createJdkDynamicProxy(final Object delegate) {
return (Subject) Proxy.newProxyInstance(Thread.currentThread()
.getContextClassLoader(),
new Class<?>[] {Subject.class}, new MyInvocation(delegate));
} private static class MyInvocation implements InvocationHandler { final private Object delegate; MyInvocation(Object delegate) {
this.delegate = delegate;
} @Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// System.out.println("aop before real request!");
return method.invoke(delegate, args);
// System.out.println("aop after real request!");
//return null;
}
} public static void main(String args[]) {
Subject delegate = new RealSubject();
Subject proxy = createJdkDynamicProxy(delegate);
proxy.request();
}
}
cglib:
package com.sunchao.cglibproxy; import java.lang.reflect.Method; import com.sunchao.jdkdyproxy.*; import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy; public class CglibProxy { public static Subject createCglibDynamicProxy(final Object delegate) {
Enhancer enhancer = new Enhancer();
enhancer.setCallback(new CglibInterceptor(delegate));
enhancer.setInterfaces(new Class<?>[]{Subject.class});
Subject cglibProxy = (Subject) enhancer.create();
return cglibProxy;
} private static class CglibInterceptor implements MethodInterceptor { final private Object delegate; CglibInterceptor(Object delegate) {
this.delegate =delegate;
} @Override
public Object intercept(Object arg0, Method arg1, Object[] arg2,
MethodProxy arg3) throws Throwable {
//System.out.println("aop before do with the request!");
return arg1.invoke(delegate, arg2);
// System.out.println("aop before do with the request!");
//return null;
}
} public static void main(String args[]) {
Subject realSubject = new RealSubject();
Subject cglibProxy = createCglibDynamicProxy(realSubject);
cglibProxy.request(); }
}
javasist dynamic proxy:
package com.sunchao.javasistproxy; import java.lang.reflect.Method; import com.sunchao.jdkdyproxy.Subject; import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject; public class ClientProxy { public static Subject createJavasistDynamicProxy(final Subject delegate)
throws Exception { ProxyFactory factory = new ProxyFactory();
factory.setInterfaces(new Class<?>[]{com.sunchao.jdkdyproxy.Subject.class});
Class<?> proxyClass = factory.createClass();
Subject javasistProxy = (Subject) proxyClass.newInstance();
((ProxyObject)javasistProxy).setHandler(new JavasistInterceptor(delegate));
return javasistProxy;
} private static class JavasistInterceptor implements MethodHandler {
final private Object delegate; JavasistInterceptor(Object delegate) {
this.delegate = delegate;
} @Override
public Object invoke(Object arg0, Method arg1, Method arg2,
Object[] arg3) throws Throwable {
//System.out.println("Aop before the real request");
return arg1.invoke(delegate, arg3);
// System.out.println("Aop after the real request");
// return null;
} }
}
javasist bytecode proxy:
package com.sunchao.javasistproxy; import java.lang.reflect.Field; import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtNewConstructor;
import javassist.CtNewMethod;
import com.sunchao.jdkdyproxy.RealSubject;
import com.sunchao.jdkdyproxy.Subject; public class ByteCodeProxy { public static Subject createJavasistBytecodeDynamicProxy(final Subject delegate) throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass proxyClass = pool.makeClass(Subject.class.getName() + "JavasistProxy");
proxyClass.addInterface(pool.get(Subject.class.getName()));
proxyClass.addConstructor(CtNewConstructor.defaultConstructor(proxyClass));
proxyClass.addField(CtField.make("private " + Subject.class.getName() + " delegate ;", proxyClass));
proxyClass.addMethod(CtNewMethod.make(
"public void request() { delegate.request();}", proxyClass));
Class<?> clazz = proxyClass.toClass();
Subject bytecodeProxy = (Subject) clazz.newInstance();
Field field = bytecodeProxy.getClass().getDeclaredField("delegate");
field.setAccessible(true);
field.set(bytecodeProxy,delegate);
return bytecodeProxy;
} public static void main(String args[]) throws Exception {
Subject delegate = new RealSubject();
Subject bytecodeProxy = createJavasistBytecodeDynamicProxy(delegate);
bytecodeProxy.request();
} }
asm:
package com.sunchao.asm; import java.lang.reflect.Field;
import java.nio.ReadOnlyBufferException; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes; import com.sunchao.jdkdyproxy.RealSubject;
import com.sunchao.jdkdyproxy.Subject; public class ASMProxy { public static Subject createAsmByteCodeDynamicProxy(Subject delegate) throws Exception {
ClassWriter classWriter = new ClassWriter(true);
String className = Subject.class.getName() + "AsmProxy";
String classPath = className.replace('.', '/');
String interfacePath = Subject.class.getName().replace('.', '/'); classWriter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, classPath, null,
"java/lang/Object", new String[] {interfacePath});
MethodVisitor initVistor = (MethodVisitor) classWriter.visitMethod(Opcodes.ACC_PUBLIC,
"<init>", "()V", null, null);
initVistor.visitCode();
initVistor.visitVarInsn(Opcodes.ALOAD, 0);
initVistor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
initVistor.visitInsn(Opcodes.RETURN);
initVistor.visitMaxs(0, 0);
initVistor.visitEnd(); FieldVisitor fieldVisitor = classWriter.visitField(Opcodes.ACC_PRIVATE, "delegate" ,
"L" + interfacePath + ";" , null, null);
fieldVisitor.visitEnd(); MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "request", "()V", null, null);
methodVisitor.visitCode();
methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
methodVisitor.visitFieldInsn(Opcodes.GETFIELD, classPath, "delegate", "L" + interfacePath + ";");
methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE, interfacePath, "request", "()V");
methodVisitor.visitInsn(Opcodes.RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd(); classWriter.visitEnd();
byte[] code = classWriter.toByteArray();
Subject asmProxy = (Subject) new ByteArrayClassLoader().getClass(className, code).newInstance();
Field field = asmProxy.getClass().getDeclaredField("delegate");
field.setAccessible(true);
field.set(asmProxy, delegate);
return asmProxy; }
private static class ByteArrayClassLoader extends ClassLoader { ByteArrayClassLoader() {
super(ByteArrayClassLoader.getSystemClassLoader());
} public synchronized Class<?> getClass(String name, byte[] code) {
if(name == null)
throw new IllegalArgumentException("name == null");
return defineClass(name, code, 0, code.length);
}
} public static void main(String args[]) throws Exception{
Subject real = new RealSubject();
Subject asmProxy =createAsmByteCodeDynamicProxy(real);
asmProxy.request();
}
}
四种比较:
package com.sunchao.reflecttest; import java.text.DecimalFormat; import com.sunchao.asm.ASMProxy;
import com.sunchao.cglibproxy.CglibProxy;
import com.sunchao.javasistproxy.ByteCodeProxy;
import com.sunchao.jdkdyproxy.ClientProxy;
import com.sunchao.jdkdyproxy.RealSubject;
import com.sunchao.jdkdyproxy.Subject;
/**
*
* @author Administrator
*
*/
public class DynamicProxyTest { public static void main(String args[]) throws Exception {
Subject delegate = new RealSubject();
long time = System.currentTimeMillis();
Subject jdkProxy = ClientProxy.createJdkDynamicProxy(delegate);
time = System.currentTimeMillis() - time;
System.out.println("create jdk dynamic proxy : " + time + " ms"); time = System.currentTimeMillis();
Subject cglibProxy = CglibProxy.createCglibDynamicProxy(delegate);
time = System.currentTimeMillis() - time;
System.out.println("create cglib dynamic proxy : " + time + " ms"); time = System.currentTimeMillis();
Subject javasistProxy = com.sunchao.javasistproxy.ClientProxy.createJavasistDynamicProxy(delegate);
time = System.currentTimeMillis() - time;
System.out.println("create javasist dynamic proxy : " + time + " ms"); time = System.currentTimeMillis();
Subject javasistbytecodeProxy = ByteCodeProxy.createJavasistBytecodeDynamicProxy(delegate);
time = System.currentTimeMillis() - time;
System.out.println("create javasist bytecode proxy : " + time + " ms" ); time = System.currentTimeMillis();
Subject asmProxy = ASMProxy.createAsmByteCodeDynamicProxy(delegate);
time = System.currentTimeMillis() - time;
System.out.println("create asm bytecode proxy : " + time + " ms");
System.out.println("<============================================>");
for(int i = 0; i < 3; i++){
test(jdkProxy, "run the jdkDynamicProxy : ");
test(cglibProxy, "run the cglibDynamicProxy : ");
test(javasistProxy, "run the javasistProxy : ");
test(javasistbytecodeProxy, "run the javasist byte code proxy : ");
test(asmProxy, "run the asm byte code proxy : ");
System.out.println("<-------------------------------------->");
} } public static void test(Subject delegate, String label) {
delegate.request();//warm up
int count = 10000000;
long time = System.currentTimeMillis();
for(int i = 0; i < count; i++){
delegate.request();
}
time = System.currentTimeMillis() - time;
System.out.println(label + time + " ms, " + new DecimalFormat().format(count * 1000 / time) + " t/s");
} } 四种比较结果:
create jdk dynamic proxy : 9 ms
create cglib dynamic proxy : 169 ms
create javasist dynamic proxy : 109 ms
create javasist bytecode proxy : 375 ms
create asm bytecode proxy : 9 ms
<============================================>
run the jdkDynamicProxy : 864 ms, 1,632,020 t/s
run the cglibDynamicProxy : 794 ms, 1,775,901 t/s
run the javasistProxy : 816 ms, 1,728,021 t/s
run the javasist byte code proxy : 80 ms, 17,625,817 t/s
run the asm byte code proxy : 76 ms, 18,553,492 t/s
<-------------------------------------->
run the jdkDynamicProxy : 707 ms, 1,994,434 t/s
run the cglibDynamicProxy : 713 ms, 1,977,651 t/s
run the javasistProxy : 862 ms, 1,635,806 t/s
run the javasist byte code proxy : 66 ms, 21,364,627 t/s
run the asm byte code proxy : 73 ms, 19,315,964 t/s
<-------------------------------------->
run the jdkDynamicProxy : 740 ms, 1,905,493 t/s
run the cglibDynamicProxy : 693 ms, 2,034,726 t/s
run the javasistProxy : 844 ms, 1,670,693 t/s
run the javasist byte code proxy : 74 ms, 19,054,937 t/s
run the asm byte code proxy : 83 ms, 16,988,739 t/s
<-------------------------------------->
四种各种调用10000000次: 从创建动态代理类的时间可以看出:jdk和asm效果最优,javasist bytecode次之,在其次cglib,最差的是javasist dynamic proxy
从代理调用的时间可以看出:asm和javasist差不多最好,其他三个差不多,差距较大;
asm比较底层,对虚拟机指令要求高,综合javasist bytecode性能综合比较好,
Java 几种动态代理实现及其性能比较的更多相关文章
- java两种动态代理方式的理解
要理解动态代理,不妨先来看看一个静态代理的例子. 一.静态代理 以一个电商项目的例子来说明问题,比如我定义了一个订单的接口IOrder,其中有一个方法时delivery,代码如下. package c ...
- 使用Java中的动态代理实现数据库连接池
2002 年 12 月 05 日 作者通过使用JAVA中的动态代理实现数据库连接池,使使用者可以以普通的jdbc连接的使用习惯来使用连接池. 数据库连接池在编写应用服务是经常需要用到的模块,太过频繁的 ...
- [编织消息框架][JAVA核心技术]jdk动态代理
需要用到的工具 jdk : javac javap class 反编译 :JD-GUI http://jd.benow.ca/ import java.lang.reflect.Invocation ...
- 杨晓峰-Java核心技术-6 动态代理 反射 MD
目录 第6讲 | 动态代理是基于什么原理? 典型回答 考点分析 知识扩展 反射机制及其演进 动态代理 精选留言 Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAnd ...
- Jmh测试JDK,CGLIB,JAVASSIST动态代理方式的性能
前言 JDK,CGLIB,JAVASSIST是常用的动态代理方式. JDK动态代理仅能对具有接口的类进行代理. CGLIB动态代理方式的目标类可以没有接口. Javassist是一个开源的分析.编辑和 ...
- 一文读懂Java中的动态代理
从代理模式说起 回顾前文: 设计模式系列之代理模式(Proxy Pattern) 要读懂动态代理,应从代理模式说起.而实现代理模式,常见有下面两种实现: (1) 代理类关联目标对象,实现目标对象实现的 ...
- Java基础-CGLIB动态代理
JDK的动态代理机制只能代理实现了接口的类,而不能实现接口的类就不能实现JDK的动态代理,cglib是针对类来实现代理的,他的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,但因为采用的是继 ...
- java 笔记(3) —— 动态代理,静态代理,cglib代理
0.代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口. 代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等. 代理类与委托类之间通常会存 ...
- Java反射和动态代理
Java反射 反射机制 RTTI 编译器在编译时打开和检查*.class文件 反射机制 运行时打开和检查*.class文件 Java反射常见的方法 java反射的应用 setAccessible(bo ...
随机推荐
- Vue 爬坑之路(五)—— 组件进阶
组件(Component)是 Vue.js 最强大的功能之一,之前的文章都只是用到了基本的封装功能,这次将介绍一些更强大的扩展. 一.基本用法 在使用 vue-cli 创建的项目中,组件的创建非常方便 ...
- swiper轮播问题之二:默认显示3张图片,中间显示全部两边显示部分
其二:项目遇到比较有点要求的轮播图,默认显示3张图片,中间显示全部,两边显示部分.如图: 网上找了也没有找到合适的,最后经过自己摸索写了出来,贴出代码分享给大家. CSS .swipe ...
- CSS3 radial-gradient 径向渐变属性 实现重复半圆角内边框
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8gAAADiCAIAAAAd73mYAAAG+ElEQVR4nO3dQQrkNhCG0TntHGGu4U ...
- 模拟uClinux系统调用
这篇文章原来放在CU上的,现在挪过来了.CU上设置不可见了. 1. 目标 这里主要是实验一下uclinux的系统调用. 2. 环境 OS :vmware + red ...
- c# 岛2 小辅助~~~ 钓鱼 连击
- asp.net mvc 客户端验证
插件 jQuery unobtrusive Validation @Html.TextBoxFor(x=>x.UserName) [StringLength(7,MinimumLength=2, ...
- 【网络流】POJ1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78671 Accepted: 3068 ...
- master_pos_wait函数与MySQL主从切换
背景 主从切换是高可用MySQL架构的必要步骤(即使用不发生,也要有备无患).一般设置为双M(M1.M2),假设当前状态为写M1,而M2只读,切换的大致流程如下: 1. 停止应用写M1,将M1设置为 ...
- Git学习随笔
前期准备事项: 1.注册GitHub账号,注册地址:https://github.com 2.下载Git for Windows工具,下载地址:http://gitforwindows.org,软件安 ...
- H5+Ajax+WebApi实现文件下载(进度条,多文件)
前言 踩过的坑 1.WebAPI跨域 2.Jquery ajax低版本不支持XHR 2功能 3.Jquery ajax不支持Deferred的process事件 4.IE下文件名乱码问题 功能实现 & ...