白季飞龙的个人主页

Java动态代理

示例代码

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

/**
 * Created by BaiJiFeiLong@gmail.com at 2018/4/17 11:15
 */
public class App {

    interface HiService {
        String hi(String name);
    }

    public static void main(String[] args) {
        proxy();
    }

    private static void proxy() {
        HiService hiService = (HiService)Proxy.newProxyInstance(
            App.class.getClassLoader(),
            new Class[]{HiService.class},
            new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) {
                    return "Hi ~ " + args[0];
                }
            });
        String str = hiService.hi("world");
        System.out.println("str = " + str);
    }

}

文章首发: https://baijifeilong.github.io


漫漫路,莫论逍遥;潜心修,只为悟道