我在使用Java的动态代理的时候,implements InvocationHandler,实现它的方法invoke,代码是这样的:    
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
        Object result = null;
                
        System.out.println("before invoke method :" + method.getName());
        
        result =  method.invoke(this.targetObj, args);
        
        System.out.println("after invoke method : " + method.getName());
        
        return result;
}---------------------------------------------------------------- 但是在运行后,在控制台是这样的显示: before invoke method :getTheNum 
after invoke method : getTheNum 
the number is : 100 
为什么会先执行1.3句,然后再执行第二句?? 请大侠赐教!

解决方案 »

  1.   

    执行顺序是123,只是你的 the number is是result出去输出后输出的。
    你是要实现拦截器模式吧。你这样写就可以看到与执行顺序一样的输出了public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
    {
            Object result = null;
                    
            System.out.println("before invoke method :" + method.getName());
            
            result =  method.invoke(this.targetObj, args);
            System.out.println(result);//转成适当的类型        System.out.println("after invoke method : " + method.getName());
            
            return result;
    }
      

  2.   

    the number is : 100 //这个在哪里打印的?方法返回之后?那你这个顺序理所当然的了
      

  3.   

    the number is : 100 
    这个应该是return result;
    后返回执行的
    所以顺序没有问题
      

  4.   

    the number is : 100 
    这个应该是return result;
    后返回执行的
    所以顺序没有问题
      

  5.   

    the number is : 100   这句什么时候执行的。
      

  6.   

    method.invoke(this.targetObj, args); 
    路过,学习了,谢谢楼主了..
      

  7.   

    method.invoke(this.targetObj, args);楼主确定这个方法里面有输出语句?
      

  8.   


    嗯,对.首先是买main方法:public static void main(String[] args)
    {
        ProxyClass proxyClass = new ProxyClass();
        targetInterface targetInterface =  proxyClass.bind(new targetClass(100));
        System.out.println("the number is : " + targetInterface.getTheNum() );
    }
    然后是ProxyClass类:public class ProxyClass implements InvocationHandler
    {    private targetInterface targetObj;
        
        public targetInterface bind(targetInterface targetObj)
        {
            this.targetObj = targetObj;
            
            //  返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序。
            return (targetInterface)Proxy.newProxyInstance
                    (targetObj.getClass().getClassLoader(), targetObj.getClass().getInterfaces(), this);
        }
        
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
        {
            Object result = null;
                    
            System.out.println("before invoke method :" + method.getName());
            
            result =  method.invoke(this.targetObj, args);
            
            System.out.println("after invoke method : " + method.getName());
            
            return result;
        }
    }
    在main中将被代理对象做为参数传入bind方法中,bind方法生成一个代理类实例.
    因为是实现了invokeHandler接口,所以接下来会去执行invoke这个方法.方法中有句method.invoke(this.targetObj, args);
    当执行到main方法中的第三句System.out.println("the number is : " + targetInterface.getTheNum() );
    时,会用去执行代理类中的invoke调用...以上是我自己的分析.另外.非常感谢LS所有大大的关注.感谢感谢.