1、JDK代理示例

JDK动态代理只能对实现了接口的类生成代理,不能针对类代理,JDK动态代理需要如下类:接口、实现类、实现InvocationHandler接口的代理类(这个类中包括以下方法:可以传入目标对象的构造函数,InvocationHandler接口方法的实现可以执行目标方法,得到目标对象的代理对象)

代码示例如下:接口

package jicu.proxy.jdk;

public interface Print {

	void printWords();

	void printNumbers();

}

实现类:

package jicu.proxy.jdk;

public class PrintImpl implements Print {

	public PrintImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void printWords() {
		// TODO Auto-generated method stub
		System.out.println("this is PrintImpl printWords()");

	}

	@Override
	public void printNumbers() {
		// TODO Auto-generated method stub
		System.out.println("this is PrintImpl printNumbers()");

	}

}

代理类:

package jicu.proxy.jdk;

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

public class JdkProxy implements InvocationHandler {

	private Object object;

	/**
	 * 构造方法传入目标对象
	 *
	 * @param object
	 */
	public JdkProxy(Object object) {
		// TODO Auto-generated constructor stub
		this.object = object;
	}

	/**
	 * 执行目标对象的方法
	 */
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("before method invoke");
		// 执行目标对象的方法
		Object o = method.invoke(object, args);
		System.out.println("after method invoke");
		return o;
	}

	/**
	 * 获取目标对象的代理对象
	 *
	 * @return
	 */
	public Object getProxy() {
		return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), object.getClass().getInterfaces(),
				this);
	}

}

测试方法:

package jicu.proxy.jdk;

public class TestMain {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 目标对象
		Print print = new PrintImpl();
		// 代理类
		JdkProxy jdkProxy = new JdkProxy(print);
		// 代理对象
		Print printProxy = (Print) jdkProxy.getProxy();
		printProxy.printWords();
		printProxy.printNumbers();

	}

}

2、cglib是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中的方法,其底层是通过使用一个小而块的字节码处理框架ASM,来转换字节码并生成新的类

cglib代理示例

package soundsystem;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

public class CglibProxy {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(CglibProxy.class);
		enhancer.setCallback(new MethodInterceptorImpl());
		// 生成代理对象
		CglibProxy cgPxocy = (CglibProxy) enhancer.create();
		cgPxocy.test();

	}

	public void test() {
		// TODO Auto-generated method stub
		System.out.println("this is CglibProxy test()");
	}

	/**
	 * 通过代理对象调用目标方法
	 *
	 * @author timi
	 *
	 */
	private static class MethodInterceptorImpl implements MethodInterceptor {

		public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
			// TODO Auto-generated method stub
			System.out.println("before invoke");
			Object o = proxy.invokeSuper(obj, args);
			System.out.println("after invoke");
			return o;
		}

	}

}
03-28 04:45