---
title: "cglib中Enhancer的简单使用"
author: "huttoncs"
date: "2014-07-17T16:21:23Z"
tags: []
categories: ["企业架构"]
slug: "cglib-enhancer"
draft: false
lastmod: "2014-07-17T16:21:23Z"
id: 2093222
url: "https://huttoncs.iteye.com/blog/2093222"
---

JDK自从1.3版本开始，就引入了动态代理，JDK的动态代理用起来非常简单，但是它有一个限制，就是使用动态代理的对象**必须实现一个或多个接口 。如果想代理没有实现接口的类可以使用CGLIB包。**  
　　CGLIB是一个强大的高性能的代码生成包。它被许多AOP的框架（例如Spring AOP）使用，为他们提供方法的interception（拦截）。Hibernate也使用CGLIB来代理单端single-ended(多对一和一对一)关联。EasyMock通过使用模仿（moke）对象来测试java代码的包。它们都通过使用CGLIB来为那些没有接口的类创建模仿（moke）对象。  
　　CGLIB包的底层是通过使用一个小而快的字节码处理框架ASM，来转换字节码并生成新的类。不鼓励直接使用ASM，因为它要求你必须对JVM内部结构包括class文件的格式和指令集都很熟悉

 

以下为模拟案例，在使用CGLIB时需引入cglib-nodep-2.1\_3.jar包

简单的使用方法

Enhancer中有几个常用的方法, setSuperClass和setCallback, 设置好了SuperClass后, 可以使用create制作代理对象了

Enhancer enhancer = new Enhancer();    
enhancer.setSuperclass(EnhancerDemo.class);  

enhancer.setCallback(new MethodInterceptorImpl());    
EnhancerDemo demo = (EnhancerDemo) enhancer.create(); 

 

 

实现MethodInterceptor接口

private static class MethodInterceptorImpl implements MethodInterceptor {  

        @Override   
        public Object intercept(Object obj, Method method, Object\[] args,    
                MethodProxy proxy) throws Throwable {    
            System.out.println("Before invoke " + method);    
            Object result = proxy.invokeSuper(obj, args);    
            System.out.println("After invoke" + method);    
            return result;    
        }      
    } 

intercept方法, Object result = proxy.invokeSuper(obj, args)调用了原来的方法, 在这个调用前后可以添加其他的逻辑, 相当于AspectJ的around

完整代码如下: 

import java.lang.reflect.Method;    
import net.sf.cglib.proxy.Enhancer;    
import net.sf.cglib.proxy.MethodInterceptor;    
import net.sf.cglib.proxy.MethodProxy;  

public class EnhancerDemo {    
    public static void main(String\[] args) {    
        Enhancer enhancer = new Enhancer();    
        enhancer.setSuperclass(EnhancerDemo.class);    
        enhancer.setCallback(new MethodInterceptorImpl());    
             
        EnhancerDemo demo = (EnhancerDemo) enhancer.create();    
        demo.test();             
        System.out.println(demo);    
    }    
         
    public void test() {    
        System.out.println("EnhancerDemo test()");    
    }  

     
    private static class MethodInterceptorImpl implements MethodInterceptor {    
        @Override   
        public Object intercept(Object obj, Method method, Object\[] args,    
                MethodProxy proxy) throws Throwable {    
            System.err.println("Before invoke " + method);    
            Object result = proxy.invokeSuper(obj, args);    
            System.err.println("After invoke" + method);    
            return result;    
        }              
    }  

}