Web基础(十一)----动态代理

一、什么是代理(中介)

目标对象/被代理对象 ------ 房主:真正的租房的方法
代理对象 ------- 黑中介:有租房子的方法(调用房主的租房的方法)
执行代理对象方法的对象 ---- 租房的人 
流程:我们要租房----->中介(租房的方法)------>房主(租房的方法)
抽象:调用对象----->代理对象------>目标对象


二、动态代理

动态代理:不用手动编写一个代理对象,不需要一一编写与目标对象相同的方法,这个过程,在运行时的内存中动态生成代理对象。------字节码对象级别的代理对象
动态代理的API:
在jdk的API中存在一个Proxy中存在一个生成动态代理的的方法newProxyInstance


案例一:

Target

package com.ken.proxy;
public class Target implements TargetInterface {
	@Override
	public void method1() {
		System.out.println("method1 running...");
	}
	@Override
	public String method2() {
		System.out.println("method2 running...");
		return "method2";
	}
}
TargetInterface
package com.ken.proxy;
public interface TargetInterface {
	public void method1();
	public String method2();
}
ProxyTest
package com.ken.proxy;

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

import org.junit.Test;

public class ProxyTest {

	@Test
	public void test1() {
		InvocationHandler h;
		// 获得动态的代理对象----在运行时 在内存中动态的为Target创建一个虚拟的代理对象
		// objProxy是代理对象 根据参数确定到底是谁的代理对象
		TargetInterface objProxy = (TargetInterface)Proxy.newProxyInstance(
				Target.class.getClassLoader(), // 与目标对象相同的类加载器
				new Class[] { TargetInterface.class }, new InvocationHandler() {
					// invoke:代表的是执行代理对象的方法
					@Override
					// method:代表目标对象的方法字节码对象
					// args:代表目标对象的相应的方法的参数
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						System.out.println("目标方法前的逻辑");
						// 执行目标对象的方法
						Object invoke = method.invoke(new Target(), args);
						System.out.println("目标方法后的逻辑");
						return invoke;
					}
				});

		objProxy.method1();
		String method2 = objProxy.method2();
		System.out.println(method2);
	}
}

案例二:

Target

public class Target implements TargetInterface{
	@Override
	public void method1() {
		System.out.println("method1 running...");
	}
	@Override
	public String method2() {
		System.out.println("method2 running...");
		return "method2";
	}
	@Override
	public int method3(int x) {
		return x;
	}
}
TargetInterface
public interface TargetInterface {
	public void method1();
	public String method2();
	public int method3(int x);
}
ProxyTest2
public class ProxyTest2 {
	public static void main(String[] args) {
		final Target target = new Target();
		//动态创建代理对象
		TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
				target.getClass().getClassLoader(), 
				target.getClass().getInterfaces(), 
				new InvocationHandler() {
					@Override
					//被执行几次?------- 看代理对象调用方法几次
					//代理对象调用接口相应方法 都是调用invoke
					/*
					 * proxy:是代理对象
					 * method:代表的是目标方法的字节码对象
					 * args:代表是调用目标方法时参数
					 */
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						//反射知识点
						Object invoke = method.invoke(target, args);//目标对象的相应方法
						//retrun返回的值给代理对象
						return invoke;
					}
				}
			);
		proxy.method1();//调用invoke---Method:目标对象的method1方法  args:null  返回值null
		String method2 = proxy.method2();//调用invoke---Method:目标对象的method2方法  args:null  返回值method2
		int method3 = proxy.method3(100);调用invoke-----Method:目标对象的method3方法 args:Object[]{100}  返回值100
		System.out.println(method2);
		System.out.println(method3);	
	}
}

注意:JDK的Proxy方式实现的动态代理,目标对象必须有接口,没有接口不能实现jdk版动态代理。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值