cglib動態代理

來源:互聯網
上載者:User

標籤:區別   object   ati   架構   invoke   owa   library   log   使用   

代理即為訪問對象添加一層控制層,使其間接化,控制層可以為對象訪問添加操作屬性。

cglib:Code Generation library,

  • 基於ASM(java位元組碼作業碼)的高效能代碼產生包
  • 被許多AOP架構使用
  • 區別於JDK動態代理,cglib不需要實現介面。

執行個體:

 1  import java.lang.reflect.Method;   2     3  import net.sf.cglib.proxy.Enhancer;   4  import net.sf.cglib.proxy.MethodInterceptor;   5  import net.sf.cglib.proxy.MethodProxy;   6     7     8  public class MyMethodInterceptor implements MethodInterceptor {         9      10  11      public String myFun(String arg){  12          return "hello," + arg ;  13      }  14       15      public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {  16          String methodName = method.getName();  17           18          System. out .println( "[intercept] fun invoked before" );  19          String result = (String)args[0] + "..." ;  20          System. out .println( result );  21          System. out .println( "[intercept] fun invoked after" );  22          return result;       23      }  24       25      public Object createProxy(){  26          Enhancer enhancer = new Enhancer();  27          enhancer.setSuperclass(MyMethodInterceptor. class );  28          enhancer.setCallback( this );  29          return enhancer.create();  30      }  31       32       33      public static void main(String[] args) {  34          MyMethodInterceptor ss = new MyMethodInterceptor();  35          MyMethodInterceptor proxy = (MyMethodInterceptor)ss.createProxy();  36 37          c1.myFun( "cglib test" );  38           39      }  40    41  }  

 

cglib動態代理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.