动态代理

	/**
	 * @param args 
	 * @throws  <span style="font-family:Arial, Helvetica, sans-serif;">Exception</span>
	 */
	public static void main(String[] args) throws Exception {
//		用动态方式生成一个Collection的实现类(参数里面需要一个类加载器,和一个接口的字节码对象)
		Class clazzProxyClass=Proxy.getProxyClass(Collection.class.getClassLoader(), Collection.class);
		//获取这个动态类的所有的构造方法
		Constructor[] constructors=clazzProxyClass.getConstructors();
		System.out.println("----constructor----list----");
		for(Constructor constructor:constructors){
			String name=constructor.getName();
			StringBuilder sb=new StringBuilder(name);
			sb.append("(");
			//获取构造方法中的所有的参数
			Class[] params=constructor.getParameterTypes();
			for(Class param:params){
				//两个参数中间用","号隔开
				sb.append(param.getName()).append(",");
			}
			//如果没有参数,不需要删掉最后一个","号,否则需要删除
			if(params.length!=0&&params!=null){
				sb.deleteCharAt(sb.length()-1);  
			}
			sb.append(")");
			System.out.println(sb.toString());
			
		}
		
		Method[] methods=clazzProxyClass.getMethods();
		System.out.println("----methods----list----");
		for(Method method:methods){
			String name=method.getName();
			StringBuilder sb=new StringBuilder(name);
			sb.append("(");
			Class[] params=method.getParameterTypes();
			for(Class param:params){
				sb.append(param.getName()).append(",");
			}
			if(params.length!=0&&params!=null){
				sb.deleteCharAt(sb.length()-1);
			}
			sb.append(")");
			System.out.println(sb.toString());
		}
		//创建动态类的实例对象
		class InvocationHandler1 implements InvocationHandler{
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				return null;
			}
		}
		Constructor constructor=clazzProxyClass.getConstructor(InvocationHandler.class);
		Collection proxy1=(Collection) constructor.newInstance(new InvocationHandler1());
		System.out.println(proxy1);
		Collection proxy2=(Collection) constructor.newInstance(new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable {
				return null;
			}
		});
		Collection proxy3=(Collection)Proxy.newProxyInstance(Collection.class.getClassLoader(),
				new Class[]{Collection.class},
				new InvocationHandler() {
					ArrayList target=new ArrayList();
					@Override
					public Object invoke(Object proxy, Method method, Object[] args)
							throws Throwable {
						Object retVal=method.invoke(target, args);
						return retVal;
					}
				});
		proxy3.add("asd");
		proxy3.add("qwe");
		proxy3.add("ghj");
		System.out.println(proxy3.size());
		}
	

代理:

为其他对象提供一种代理以控制对这个对象的访问。在一些情况下客户不想或者不能直接引用一个目标对象,

而代理对象可以在客户和目标对象之间起到中介作用,去掉客户不能看到的内容和服务或者增添客户需要的额外服务。

就是通过proxy类的getProxyInstance方法,传入一个类加载器,一个接口的字节码文件,还有一个InvocationHandler对象,通过代理调用invoke方法,在调用目标对象的方法,再将目标对象方法的返回值返回给代理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值