这是网上流行的以篇AOP入门的例子
我问的不是AOP,而是动态代理
首先请看LogHandler中的invoke方法
这个invoke中第一个参数object proxy有什么用呢?
我看过许多类似的InvocationHandler实现程序
这个proxy也是根本不用的,而jdk说"proxy - the proxy instance that the method was invoked on"
难道会自动调吗?.不是把?public class LogHandler implements InvocationHandler { 
     
    private Logger logger = Logger.getLogger(this.getClass().getName()); 
     
    private Object delegate; 
     
    public LogHandler(Object delegate){ 
        this.delegate = delegate; 
    } 
     
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
         
        Object o = null; 
        try {             
         
            logger.info("method stats..." + method); 
            o = method.invoke(delegate,args); 
            logger.info("method ends..." + method); 
         
        } catch (Exception e){          
            logger.info("Exception happends..."); 
            //excetpion handling. 
        } 
        return o; 
    } 
     
} public class BusinessObject implements BusinessInterface { 
     
    private  Logger logger = Logger.getLogger(this.getClass().getName()); 
     
    public void processBusiness(){ 
         
        //business processing 
    System.out.println(“here is business logic”);        
    } 
} 客户端调用商业方法的代码如下, 
BusinessInterface businessImp = new BusinessObject(); 
         
InvocationHandler handler = new LogHandler(businessImp); 
         
BusinessInterface proxy = (BusinessInterface) Proxy.newProxyInstance( 
            businessImp.getClass().getClassLoader(), 
            businessImp.getClass().getInterfaces(), 
            handler); proxy.processBusiness(); 这里的Proxy.newProxyInstance( 
            businessImp.getClass().getClassLoader(), 
            businessImp.getClass().getInterfaces(), 
            handler); 实在觉得jDK这个接口的参数有些多余?
            
   businessImp.getClass().getClassLoader(), 
            businessImp.getClass().getInterfaces(),完全就只要businessImp一个参数就够拉
            它内部在businessImp.getClass().getClassLoader(), 
            businessImp.getClass().getInterfaces(), 不就可以拉
            前面也不必
InvocationHandler handler = new LogHandler(businessImp); 
         只要接口Proxy.newProxyInstance(     businessImp,     handler);不也可以在里面作吗?,实在有些麻烦
         这是小弟一些粗浅的看法,求哪位老大给个说法或解释

解决方案 »

  1.   

    businessImp.getClass().getClassLoader()
    不一定非要这样得到ClassLoader,你也可以其它的ClassLoader,如果只给定businessImp,就不能用其它的ClassLoader了
      

  2.   

    动态代理,JDK中定义了接口类型的,使用obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),spring还有另一种是没有接口的cglib,继承至MethodInterceptor使用的是enhancer.setSuperclass(clz);enhancer.setCallback(this)
      

  3.   

    动态代理我也没搞清楚那个invoke方法的参数,真郁闷,愣是看不出所以然来了