jdk动态代理生成对象

package com.dq;

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

public class TestMain 
{
	public static void main(String[] args) 
	{
		// 代理类的处理器
		InvocationHandler handler = new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args)
					throws Throwable 
			{
				System.out.println("方法:" + method.getName());
				System.out.println("这里是代理类的处理器");
				return null;
			}
		};
		IClient proxyObj = (IClient)Proxy.newProxyInstance(IClient.class.getClassLoader(), new Class[]{IClient.class}, handler);
		proxyObj.method1(null);
	}
}

interface IClient	// 接口,根据这个接口动态生成代理对象
{
	void method1(String msg);
}
发布了236 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/gunsmoke/article/details/104738566