1. 程式人生 > >使用Java實現動態代理工具包

使用Java實現動態代理工具包

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

public class ProxyFactory {
	private Object target;

	public ProxyFactory(Object target) {
		this.target = target;
	}

	public Object getProxyInstance() {
		return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
				new InvocationHandler() {
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						System.out.println("AAAAAAAA");
						Object result = method.invoke(target, args);
						System.out.println("BBBBBBBBBBBB");
						return result;
					}
				});
	}
}