java动态代理

参数说明

Object obj =  Proxy.newProxyInstance(loader, interfaces, new InvocationHandler() {
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	    Object object = method.invoke(new Person(), args);
            return object;
	}
});

loader:需要代理接口的类加载器

interfaces:实现功能的类所实现的接口类。new Class[]{} ()

new InvocationHandler匿名对象,实现接口中的方法。

invoke中的参数说明:

proxy:代理对象

method:所调用的方法。

args:方法的参数

object:方法的返回值

案例:

需要代理的接口

public interface Daily {
	void run();
	int eat();
}

public interface Play {
	void playGame();
	void playBall();
}

 Person类(实现接口)

public class Person implements Play,Daily{

	@Override
	public void run() {
		System.out.println("run");
	}

	@Override
	public int eat() {
		System.out.println("eat");
		return 0;
	}

	@Override
	public void playGame() {
		System.out.println("playGame");
	}

	@Override
	public void playBall() {
		System.out.println("playBall");
	}

}

测试类

 

((Daily) Proxy.newProxyInstance(Daily.class.getClassLoader(), new Class[] { Play.class, Daily.class },
	new InvocationHandler() {

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						System.out.println("方法开始前的工作"); 
						Object object = method.invoke(new Person(), args);
						System.out.println("方法执行结束");
						return object;
					}
	})).eat();
		 
((Daily)Proxy.newProxyInstance(Daily.class.getClassLoader(), new Class[] { Play.class, Daily.class },
	new InvocationHandler() {
        @Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//						System.out.println(proxy.getClass());
						System.out.println("方法开始前的工作");
						Object object = method.invoke(new Person(), args);
						System.out.println("方法执行结束");
						return object;
					}
	})).run() ;
		 
((Play) Proxy.newProxyInstance(Play.class.getClassLoader(), new Class[] { Play.class, Daily.class },
	new InvocationHandler() {
        @Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						System.out.println("方法开始前的工作");
						Object object = method.invoke(new Person(), args);
						System.out.println("方法执行结束");
						return object;
					}
	})).playBall();
((Play) Proxy.newProxyInstance(Play.class.getClassLoader(), new Class[] { Play.class, Daily.class },
	new InvocationHandler() {
        @Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						System.out.println("方法开始前的工作");
						Object object = method.invoke(new Person(), args);
						System.out.println("方法执行结束");
						return object;
					}
	})).playGame();
}

打印结果:

方法开始前的工作
eat
方法执行结束
方法开始前的工作
run
方法执行结束
方法开始前的工作
playBall
方法执行结束
方法开始前的工作
playGame
方法执行结束

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值