jdk代理测试类

//创建目标对象
        final Target target = new Target();
        //增强对象
        final Advice advice = new Advice();
        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),//目标对象类加载器
                target.getClass().getInterfaces(),//目标相同的接口字节码对象数组
                new InvocationHandler() {
                    //调用代理对象的任何方法,实质执行的都是invoke方法
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //前置增强
                        advice.before();
                        Object invoke = method.invoke(target, args);
                        //后置增强
                        advice.after();
                        return invoke;
                    }
                }
        );
        //调用代理对象的方法
        proxy.save();

更多免费技术资料可关注:annalin1203

发布了969 篇原创文章 · 获赞 5 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/u010395024/article/details/105572902
今日推荐