Java动态代理

package com;

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

/**
 * @description:
 * @author: mabh
 * @create: 2020/7/21 10:46 下午
 **/
public class aaa {
    private static ABC abc;

    public static void main(String[] args) {
        Class<ABC> proxyInterface = ABC.class;
        ABC abc = newProxyInstance(proxyInterface, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return null;
            }
        });
        abc.showHello("张三");
    }

    public static <T> T newProxyInstance(Class<T> proxyInterface, InvocationHandler invocationHandler) {
        return (T) Proxy.newProxyInstance(proxyInterface.getClassLoader(), new Class[]{proxyInterface},invocationHandler);
    }
}

package com;

/**
 * @description:
 * @author: mabh
 * @create: 2020/7/21 10:45 下午
 **/
public interface ABC {
    String showHello(String name);
}

你可能感兴趣的:(java)