zoukankan      html  css  js  c++  java
  • (转载,但不知道谁原创)获取SPRING 代理对象的真实实例,可以反射私有方法,便于测试

    /** 
    * 获取 目标对象 
    * @param proxy 代理对象 
    * @return 
    * @throws Exception 
    */ 
    public static Object getTarget(Object proxy) throws Exception { 
      if(!AopUtils.isAopProxy(proxy)) { 
        return proxy;//不是代理对象 
      } 
    
      if(AopUtils.isJdkDynamicProxy(proxy)) { 
        return getJdkDynamicProxyTargetObject(proxy); 
      } else { //cglib 
        return getCglibProxyTargetObject(proxy); 
      } 
    } 
    
    
    private static Object getCglibProxyTargetObject(Object proxy) throws Exception { 
      Field h = proxy.getClass().getDeclaredField("CGLIB$CALLBACK_0"); 
      h.setAccessible(true); 
      Object dynamicAdvisedInterceptor = h.get(proxy); 
      Field advised = dynamicAdvisedInterceptor.getClass().getDeclaredField("advised"); 
      advised.setAccessible(true); 
      Object target = ((AdvisedSupport)advised.get(dynamicAdvisedInterceptor)).getTargetSource().getTarget(); 
      return target; 
    } 
    
    
    private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception { 
      Field h = proxy.getClass().getSuperclass().getDeclaredField("h"); 
      h.setAccessible(true); 
      AopProxy aopProxy = (AopProxy) h.get(proxy); 
      Field advised = aopProxy.getClass().getDeclaredField("advised"); 
      advised.setAccessible(true); 
      Object target = ((AdvisedSupport)advised.get(aopProxy)).getTargetSource().getTarget(); 
      return target; 
    }
  • 相关阅读:
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    综合练习:词频统计
    Hadoop综合大作业
    理解MapReduce
    熟悉常用的HBase操作
    熟悉常用的HDFS操作
    爬虫大作业
    数据结构化与保存
    使用正则表达式,取得点击次数,函数抽离
  • 原文地址:https://www.cnblogs.com/sweetchildomine/p/5864296.html
Copyright © 2011-2022 走看看