java 动态代理实现

先附上项目结构:


步骤:

1.创建IFly接口:

package glut.proxy;
public interface IFly {
	void fly();
}

2.创建Bird类,并让它实现IFly:

package glut.proxy;
public class Bird implements IFly {
	public void fly() {
		System.out.println("A bird is flying");
	}
}

3.创建MyProxy类,并让它实现InvocationHandler

package glut.proxy;

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

public class MyProxy implements InvocationHandler {
	// 目标实例
	private Object targetInstance;

	// 不允许无参构造函数
	private MyProxy() {
		// TODO Auto-generated constructor stub
	}

	public MyProxy(Object targetInstance) {
		this.targetInstance = targetInstance;
	}

	/**
	 * Processes a method invocation on a proxy instance and returns the result.
	 * This method will be invoked on an invocation handler when a method is
	 * invoked on a proxy instance that it is associated with.
	 * 每次调用目标实例的方法时,会回调invoke方法
	 * @param 代理对象实例
	 * 
	 * @param 目标实例被调用的方法名
	 * 
	 * @param 目标实例被调用的方法的参数
	 * 
	 * @return 目标实例调用的方法的返回值
	 * 
	 * 
	 * @throws Throwable
	 *             the exception to throw from the method invocation on the
	 *             proxy instance. The exception's type must be assignable
	 *             either to any of the exception types declared in the
	 *             {@code throws} clause of the interface method or to the
	 *             unchecked exception types {@code java.lang.RuntimeException}
	 *             or {@code java.lang.Error}. If a checked exception is thrown
	 *             by this method that is not assignable to any of the exception
	 *             types declared in the {@code throws} clause of the interface
	 *             method, then an {@link UndeclaredThrowableException}
	 *             containing the exception that was thrown by this method will
	 *             be thrown by the method invocation on the proxy instance.
	 * 
	 * @see UndeclaredThrowableException
	 */
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		// TODO Auto-generated method stub
		beforeInvoke();

		Object invoke = method.invoke(targetInstance, args);

		afterInvoke();

		return invoke;
	}

	/**
	 * 调用之前
	 */
	private void afterInvoke() {
		// TODO Auto-generated method stub
		System.out.println("after invoke");
	}

	/**
	 * 调用之后
	 */
	private void beforeInvoke() {
		// TODO Auto-generated method stub
		System.out.println("before invoke");

	}

}
4.测试代码:

package glut.test;

import glut.proxy.Bird;
import glut.proxy.IFly;
import glut.proxy.MyProxy;

import java.lang.reflect.Proxy;

import org.junit.Test;

public class MyTest {
	@Test
	public void test() {
		IFly bird = (IFly) Proxy.newProxyInstance(IFly.class.getClassLoader(),
				new Class[] { IFly.class }, new MyProxy(new Bird()));
		bird.fly();
	}

}


5.测试结果:

before invoke
A bird is flying
after invoke



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值