欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

动态代理-初级篇

程序员文章站 2022-05-28 15:32:22
...

最近想总结一下java的动态代理,动态代理有很多种实现方案,下面介绍三种最常用的动态搭理方案:1.jdk自带动态代理2.javassist实现动态代理3.cglib实现动态代理

接口和实现类代码:

package com.java.day1.dynamicproxy.one;

public interface Interface {
	void doSomeThing();
}

 

package com.java.day1.dynamicproxy.one;

public class RealSubject   implements Interface{

	@Override
	public void doSomeThing() {
		System.out.println("do some things..");
	}
}

 jdk自带动态搭理实现

package com.java.day1.dynamicproxy.one;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class JdkProxyHandler  implements InvocationHandler{
	// 这个就是我们要代理的真实对象
    private Object subject;
    //    构造方法,给我们要代理的真实对象赋初值
    public JdkProxyHandler(Object subject) {
        this.subject = subject;
    }
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("do otherthings..jdk..");
		return method.invoke(subject, args);
 	}
}

 

package com.java.day1.dynamicproxy.one;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class JdkTest {

	public static void main(String[] args) {
		//被代理的类
		RealSubject rs = new RealSubject();
		// 代理增强类
		InvocationHandler ih = new JdkProxyHandler(rs);
		// 用java反射生成代理类
		Interface proxy = (Interface) Proxy.newProxyInstance(ih.getClass().getClassLoader(), rs.getClass().getInterfaces(), ih);
		proxy.doSomeThing();
	}

}

 javassist实现动态代理

package com.java.day1.dynamicproxy.one;

import java.lang.reflect.Method;

import javassist.util.proxy.MethodHandler;

public class JavassitInterceptor implements MethodHandler{
	private Object subject;
	public JavassitInterceptor(Object subject){
		this.subject = subject;
	}
	@Override
	public Object invoke(Object self, Method m, Method prosess, Object[] args) throws Throwable {
		System.out.println("do other thing..javassist.");
		return m.invoke(subject, args);
	}

}

 

package com.java.day1.dynamicproxy.one;


import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;

public class javassistTest {

	public static void main(String[] args) throws Exception {
		//被代理的类
		RealSubject rs = new RealSubject();
		// 代理增强类
		JavassitInterceptor ih = new JavassitInterceptor(rs);
		// 用javassist反射生成代理类
	    ProxyFactory pf = new ProxyFactory();
	    pf.setInterfaces(rs.getClass().getInterfaces());
	   Class<?>  proxyClass = pf.createClass();
	   Interface proxy = (Interface) proxyClass.newInstance();
	   ((ProxyObject)proxy).setHandler(ih);
	   proxy.doSomeThing();
 	}

}

 cglib实现动态搭理

package com.java.day1.dynamicproxy.one;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class CglibInterceptor implements MethodInterceptor {
	private Object subject;
	public CglibInterceptor(Object subject){
		this.subject = subject;
	}
	@Override
	public  Object intercept(Object object, Method method, Object[] objects,  MethodProxy methodProxy) throws Throwable  {
		System.out.println("do other things..cglib...");
		return methodProxy.invoke(subject, objects);
	}

}

 

package com.java.day1.dynamicproxy.one;


import net.sf.cglib.proxy.Enhancer;

public class CgligTest {

	public static void main(String[] args) {
		//被代理的类
		RealSubject rs = new RealSubject();
		// 代理增强类
	    Enhancer enhancer = new Enhancer();
	    CglibInterceptor  ci = new CglibInterceptor(rs);
	    enhancer.setCallback(ci);
        enhancer.setInterfaces(rs.getClass().getInterfaces());	    
        Interface proxy = (Interface)enhancer.create();
        proxy.doSomeThing();
	}

}

 我还做了实验,但从创建来看jdk是最快的cglib最慢,但创建好后调用,javassist最快,jdk次之,不过是同一个数量级的,cglib最慢比其他2种慢5倍