接口代理

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

public class FacadeProxy implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        System.out.println("接口方法调用开始");
        //执行方法
        System.out.println("method toGenericString:"+method.toGenericString());
        System.out.println("method name:"+method.getName());
        System.out.println("method args:"+(String)args[0]);
        System.out.println("接口方法调用结束");
        return "调用返回值";
    }

    @SuppressWarnings("unchecked")
	public static  T newMapperProxy(Class mapperInterface) {
        ClassLoader classLoader = mapperInterface.getClassLoader();
        Class[] interfaces = new Class[]{mapperInterface};
        FacadeProxy proxy = new FacadeProxy();
        return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);
      }

    public static void main(String[] args) {
    	IHello hello = FacadeProxy.newMapperProxy(IHello.class);
    	System.out.println(hello.sayHello("hello"));
	}
}
interface IHello{
	String sayHello(String a);
}

  

转载于:https://www.cnblogs.com/zhuxiansheng/p/10024108.html

你可能感兴趣的:(接口代理)