`
danni505
  • 浏览: 11465 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

《How does proxy do in CGLIB?》

阅读更多

Background


About 'CGLIB', we can get information form
http://cglib.sourceforge.net/

and in this article, we will use the asm framework, which website is: http://asm.ow2.org/index.html

Introduction


This proxy is not same as JDK1.2+, which main through CGLIB to do this proxy, accurately, CGLIB use their proxy to realize the method interceptor at runtime, and which work with filter make it more flexible.

(Tip:About create proxy in runtime, we can use two different techniques, CGLIB or JDK dynamic proxies, more see:http://danni505.blog.51cto.com/15547/346896)

Base of the CGLIB, which can construct an AOP of CGLib-based AOP,  which have support by spring framework.

Ok, now we have an four base opertaions for some datasource, but we need to control the delete operation just permit to daniel, and those base operations are some detail class, not show as interface, so what will you do if use proxy?

To solves the issue, here we use CGLIB proxy, the operation will contril by interceptor and the right person will control by filter, the code may be like this:

 

 



Source Code
 

package com.cglib; 

/** 
* Those are some base operation demo. 
*    
* @author daniel    cqyd505@gmail.com 
* @since 2010-07-08 11:25 
* 
*/ 
public class BaseOperations { 

  public void query() { 
    System.out.println("query"); 
  } 

  public void create() { 
    System.out.println("create"); 
  } 

  public void update() { 
    System.out.println("update"); 
  } 

  public void delete() { 
    System.out.println("delete"); 
  } 
} 

 
/** 
*    
*/ 
package com.cglib; 

import java.lang.reflect.Method; 

import net.sf.cglib.proxy.MethodInterceptor; 
import net.sf.cglib.proxy.MethodProxy; 

/** 
* Method interceptor, as you see, this is a Interceptor, which can do some job before you call some method, 
* and the key is which can be used in call back by cglib, it's real cool! 
*    
* @author daniel    cqyd505@gmail.com 
* @since 2010-07-09 09:23 
* 
*/ 
public class MyMethodInterceptor implements MethodInterceptor{ 
    
  private String name; 

  public String getName() { 
    return name; 
  } 

  public void setName(String name) { 
    this.name = name; 
  } 

  public MyMethodInterceptor(String name) { 
    this.name = name; 
  } 

  /** 
    * Here we will set our interceptor for some method. 
    * As you see the interceptor is just for daniel by name. 
    */ 
  @Override 
  public Object intercept(Object arg0, Method arg1, Object[] arg2, 
      MethodProxy arg3) throws Throwable { 
    //interceptor 
    if (this.name.equals("daniel")) { 
      return arg3.invokeSuper(arg0, arg2); 
    }else{ 
      System.out.println("Method interceptor say: haha, just daniel can do delete! you are have no permits to do this operation!"); 
                        return null; 
    } 
  } 

} 

 
package com.cglib; 

import java.lang.reflect.Method; 

import net.sf.cglib.proxy.CallbackFilter; 

/** 
* Interceptro filter, which mean this is a filter for interceptor, in other words, when app use the interceptor, filter 
* will be called first, so it's can use for abstract the logic or bussiness into the filter, mean while you can    
* set many interceptor and not case it use or not, the "CallBackFilter" will charge it, you just need to foucs 
* at what will you do in filter.    
*    
* @author daniel    cqyd505@gmail.com 
* @since 2010-07-09 10:30 
* 
*/ 
public class InterceptorFilter implements CallbackFilter{ 

  private String operationName; 
    
  @Override 
  public int accept(Method arg0) { 
    /* 
     * The accept logic 
     */ 
    if(arg0.getName().toString().equals(this.operationName)){ 
      /* 
        * if the method name is equals to the filter's , call the No:0 interceptor 
        */ 
      return 0; 
    }else{ 
      /* 
        * call No:1 interceptor 
        */ 
      return 1; 
    } 
  } 

  public InterceptorFilter(String name) { 
    this.operationName = name; 
  } 
    
} 

 
package com.cglib; 

import net.sf.cglib.proxy.Callback; 
import net.sf.cglib.proxy.Enhancer; 
import net.sf.cglib.proxy.NoOp; 

/** 
* This factory can set some interceptors into the callback of the class, not interface. 
*    
* @author daniel    cqyd505@gmail.com 
* @since 2010-07-09 09:25 
* 
*/ 
public class InterceptorFactory { 

  /** 
    * Here we will set the callback process by cglib's net.sf.cglib.proxy.Enhancer. 
    *    
    * @param interceptor 
    * @return BaseOperations 
    * @see net.sf.cglib.proxy.Enhancer 
    */ 
  public static BaseOperations getBtsOperationInstanceWithInterceptor(MyMethodInterceptor interceptor) { 
    /* 
     * This is a often and regular using of Enhancer. 
     */ 
    Enhancer enhancer = new Enhancer(); 
    enhancer.setSuperclass(BaseOperations.class); 
    /* 
     * setCallback just one interceptor 
     */ 
    enhancer.setCallback(interceptor); 
    /* 
     * set two interceptors into callback, which will used will see the filter's logic 
     */ 
    Callback[] backs =    new Callback[]{interceptor,NoOp.INSTANCE};     
    enhancer.setCallbacks(backs); 
    //set filter 
    enhancer.setCallbackFilter(new InterceptorFilter("delete")); 
    BaseOperations baseOpt = (BaseOperations) enhancer.create(); 
                return baseOpt; 
        } 
    
  /** 
    * Pay attention: 
    *         Enhancer.setCallback and setCallbacks, by check the enhancer's source codethe, we find the setCallback 
    * will call setCallbacks() to set the same "Callback[]", so if you use the setCallBack before setCallBacks    
    * in reslut, the Callback[] will fit to the last one. 
    *         Which Means, at here, ehancer have two interceptor in callback, not three!    
    */ 
    
} 

 
 
/** 
*    
*/ 
package com.cglib; 

/** 
* In this case, we will do the job by cglib, we have set some method interceptor for our operations, let's take a look! 
*    
* @author daniel    cqyd505@gmail.com 
* @since 2010-07-09 09:37 
* 
*/ 
public class CallInCase2 { 

  public static void main(String[] args) { 
    CallInCase2 c = new CallInCase2(); 
                c.doOperation(); 
        } 

  /** 
    * Do the base operations. 
    */ 
  private void doOperation() { 
    System.out.println("app run:"); 
    System.out.println("============================ console outting: ========================="); 
    //0.Create the interceptor 
    MyMethodInterceptor interceptor =    new MyMethodInterceptor("Tom"); //not ok 
    //1.Get handler by factory 
    BaseOperations basOptHandler = InterceptorFactory.getBtsOperationInstanceWithInterceptor(interceptor); 
    //2.Try do job 
    basOptHandler.create(); 
    basOptHandler.query(); 
    basOptHandler.update(); 
    basOptHandler.delete(); 
    //3.say BYEBYE 
    System.out.println("============================ app stop ========================="); 
  } 
    
} 

 



End


In fact, we just have used Enhancer, Callback, MethodInterceptor and CallbackFilter of CGLIB, it realize a proxy solution for us, and it's also be used widespread in some common use framework, if you take care about it.

(Tip:CGLIB, which API and document is so simple that I could find more using about she, so if you have any good using case, pls msn me by danni-505@hotmail.com)

 

1
0
分享到:
评论

相关推荐

    Java动态代理Proxy和cglib

    System.out.println("Doing something in the target class"); } } class MyMethodInterceptor implements MethodInterceptor { @Override public Object intercept(Object obj, Method method, Object[] args,...

    Spring如何基于Proxy及cglib实现动态代理

    Spring支持两种主要的动态代理方式:Java Proxy和cglib。这两种方法都可以用来增强或拦截目标对象的方法调用,实现如AOP(面向切面编程)的功能。 首先,我们来看Java Proxy。Java Proxy是Java内置的动态代理机制,...

    使用JDK中的Proxy技术实现AOP功能与使用CGLIB实现AOP功能

    总的来说,JDK的Proxy适用于实现了接口的目标对象,而CGLIB则适用于未实现接口的对象。两者都可以帮助我们在不修改原有代码的情况下,通过代理对象在调用方法前后插入额外的逻辑,实现AOP的功能。在Spring框架中,这...

    Cglib的jar文件 Cglib.zip

    Cglib是一个强大的Java代码生成库,它在运行时动态创建子类,为现有类提供扩展功能。这个库被广泛用于实现AOP(面向切面编程)中的代理机制,特别是当无法通过接口代理时,例如Java标准库中的类。Cglib是Eclipse的...

    动态代理cglibjar包和源码

    - JDK Proxy基于接口实现,而CGLIB基于继承,因此CGLIB可以代理没有接口的类。 - JDK Proxy性能相对较慢,因为涉及到反射,而CGLIB通过字节码生成,性能更好。 - JDK Proxy易于理解和使用,CGLIB则需要更深入的...

    Cglib3.3.0最新版jar包

    Cglib就是一种实现动态代理的方式,不同于JDK自带的Proxy,Cglib不需要目标对象实现任何接口,因此可以用于不能实现接口的对象。通过Enhancer类,我们可以指定需要代理的目标类,并提供回调方法实现动态代理逻辑。 ...

    CGLIB需要的asm-2.2.3.jar和cglib-nodep-2.2.jar

    CGLIB,全称为Code Generation Library,是一个强大的高性能的代码生成库,被广泛应用于Java世界,尤其是Spring框架中。它允许程序在运行时动态地创建Java对象并扩展已有类的功能。CGLIB是通过字节码技术实现的,而...

    cglib2.2jar包

    2. **配置**:在Spring框架中,如果需要使用CGLIB进行AOP代理,需要在配置文件中启用CGLIB代理,例如在XML配置中添加 `<aop:aspectj-autoproxy/>` 或者在Java配置中使用 `@EnableAspectJAutoProxy(proxyTargetClass ...

    CGLIB2 is not available&org.objectweb.asm.Type

    关于Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.和 java.lang.ClassNotFoundException: org.objectweb.asm.Type错误的解决方法: 就是...

    Spring cglib 中文说明

    ### Spring Cglib 中文说明 #### CGLIB 包简介与原理 CGLIB(Code Generation Library)是一个强大且高性能的代码生成库。它在众多面向切面编程(AOP)框架中扮演着核心角色,例如Spring AOP、Dynaop等。此外,它...

    Cglib所需要的jar包

    Cglib是一个强大的高性能的代码生成库,它在Java运行时可以动态地生成子类,扩展已有对象的功能,而无需实现接口或继承原有类。在Java中,动态代理主要有两种方式:一是通过Java的反射API实现,二是通过第三方库如...

    cglib.jar/cglib-nodep.jar免费下载

    **CGLib库详解** CGLib,全称Code Generation Library,是一个强大的高性能的代码生成库,它在Java世界中被广泛使用,特别是在动态代理和AOP(面向切面编程)领域。这个库允许开发者在运行时动态创建类或者增强已有...

    cglib代理模式要使用的相关jar包

    例如,`net.sf.cglib.proxy.Enhancer`是CGLIB中最常用的类,它可以用来创建代理对象。同时,`net.sf.cglib.core.Predicate`和`net.sf.cglib.core.KeyFactory`等类也是实现代理功能的重要组件。 2. asm.jar:ASM是一...

    cglib所有jar包

    JDK的Proxy类依赖于接口,只能为实现了特定接口的类生成代理,而CGlib则无需接口,通过生成目标类的子类来实现代理,因此对于没有接口的类,CGlib更具优势。 CGlib的核心组件包括: 1. **Enhancer**:这是CGlib的...

    cglib动态生成java类

    2. **无需接口**:CGLib可以对没有实现接口的类进行代理,这是Java动态代理(JDK Proxy)无法做到的。 3. **广泛支持**:许多知名框架如Spring AOP、Hibernate等都使用了CGLib作为底层的动态代理技术。 ### 应用...

    JAVA JDK静态代理、动态代理、CGlib代理的代码演示

    Java提供了两种主要的代理实现方式:JDK静态代理和动态代理,另外还有第三方库如CGlib提供的代理实现。下面我们将详细探讨这些代理技术,并通过代码演示来理解它们的工作原理。 ### 1. JDK静态代理 静态代理是我们...

    java CGLIB动态代理代理支持包。

    在java编程使用CGLIB做动态代理时需要CGLIB依赖包支持,没有这个支持包,当导入import net.sf.cglib.proxy.Enhancer;或者import net.sf.cglib.proxy.MethodProxy;类似包时会提示错误,所以分享出来同大家共勉。

    cglib-2.2.rar

    5. **Proxy**:CGLib也提供了一套与JDK的java.lang.reflect.Proxy类似的API,方便开发者使用。 在实际应用中,CGLib不仅用于动态代理,还可以用于性能优化、日志记录、测试、内存数据库等场景。例如,在单元测试中...

Global site tag (gtag.js) - Google Analytics