zoukankan      html  css  js  c++  java
  • jdk动态代理详解 二 代理失效

    在jdk动态代理详解 一 中,我们把代码做一点修改,在fun02中调用fun01

    package my.annotation.learn;
    
    public class TargetClass implements ServiceInf{
    
        @Override
        public void fun01() {
            
            System.out.println("TargetClass->fun01()");
        }
    
        @Override
        public void fun02() {
            System.out.println("TargetClass->fun02()");
            fun01();//调用fun01,方法内部的方法调用是通过this指针,也就是原对象进行调用
        }
        
        
    
    }
    

    在Demo的测试类中调用fun02

    package my.annotation.learn;
    
    import java.lang.reflect.Proxy;
    
    public class Demo {
    
        public static void main(String[] args) {
            TargetClass target=new TargetClass();
            ServiceInf proxy =(ServiceInf) Proxy.newProxyInstance(Demo.class.getClassLoader(), 
                    new Class<?>[] {ServiceInf.class}, new MyInvocationHandler(target));
            proxy.fun02();
        }
    
    }
    

    运行结果如下:

    发现fun02方法走了代理逻辑,fun01没有走代理逻辑

    代理方法开始执行
    com.sun.proxy.$Proxy0
    public abstract void my.annotation.learn.ServiceInf.fun02()
    TargetClass->fun02()
    TargetClass->fun01()
    代理方法开始结束
    

    原因就是 :方法内部的方法调用是通过this指针,也就是原对象进行调用,要走代理逻辑,必须是代理对象去调用方法。

  • 相关阅读:
    python 函数的进阶
    python 函数的基础
    python3.6 中特殊格式化 和 字典中的pop
    python 文件操作
    python 基础数据类型补充,set集合,深浅拷贝
    python is 和 == 的区别,编码问题
    python字典
    python中堆和栈_Python小知识00002
    pip的更新_Python小知识00001
    Python字符串的编码
  • 原文地址:https://www.cnblogs.com/cplinux/p/14495766.html
Copyright © 2011-2022 走看看