Nirvana Studio » Blog Archive » cglib 指南 :: 分享知识,传播技术

cglib 指南

Posted by Nicholas Ding on 六月 6th, 2006

cglib,全称是Code Generation Library,它可以用来动态继承Java类或者实现接口,很多知名的开源项目中用到了它,譬如Hibernate,Spring之类用它来实现动态代理。

增强一个已有类

public class MyClass {
 
	public void method() {
		System.out.println("MyClass.method()");
	}
}

import java.lang.reflect.Method;
 
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.MethodInterceptor;
 
public class Main {
 
	public static void main(String[] args) {
 
		Enhancer enhancer = new Enhancer();
 
		enhancer.setSuperclass(MyClass.class);
		enhancer.setCallback( new MethodInterceptorImpl() );
 
 
		MyClass my = (MyClass)enhancer.create();
 
		my.method();
	}
 
	private static class MethodInterceptorImpl implements MethodInterceptor {
		
		public Object intercept(Object obj, 
                                Method method, 
                                Object[] args, 
                                MethodProxy proxy) throws Throwable {
 
			System.out.println(method);
 
			proxy.invokeSuper(obj, args);
 
			return null;
		}
	}
}

执行结果:

public void cglib_test.MyClass.method()
MyClass.method()

使用CallbackFilter

public class MyClass {
 
	public void method() {
		System.out.println("MyClass.method()");
	}
 
	public void method2() {
		System.out.println("MyClass.method2()");
	}
}

import java.lang.reflect.Method;
 
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.NoOp;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
 
 
public class Main {
 
	public static void main(String[] args) {
 
		Callback[] callbacks =
			new Callback[] { new MethodInterceptorImpl(),  NoOp.INSTANCE };
 
		Enhancer enhancer = new Enhancer();
 
		enhancer.setSuperclass(MyClass.class);
		enhancer.setCallbacks( callbacks );
		enhancer.setCallbackFilter( new CallbackFilterImpl() );
 
 
		MyClass my = (MyClass)enhancer.create();
 
		my.method();
		my.method2();
	}
 
	private static class CallbackFilterImpl implements CallbackFilter {
 
		public int accept(Method method) {
 
			if ( method.getName().equals("method2") ) {
				return 1;
 
			} else {
				return 0;
			}
		}
	}
 
	private static class MethodInterceptorImpl implements MethodInterceptor {
		
		public Object intercept(Object obj, 
                                Method method, 
                                Object[] args, 
                                MethodProxy proxy) throws Throwable {
 
			System.out.println(method);
 
			return proxy.invokeSuper(obj, args);
		}
	}
}

执行结果:

public void cglib_test.MyClass.method()
MyClass.method()
MyClass.method2()

使用Mixin

public interface MyInterfaceA {
 
	public void methodA();
}
 
public interface  MyInterfaceB {
	public void methodB();
}
 
public class MyInterfaceAImpl implements MyInterfaceA {
 
	public void methodA() {
		System.out.println("MyInterfaceAImpl.methodA()");
	}
}
 
public class MyInterfaceBImpl implements MyInterfaceB {
 
	public void methodB() {
		System.out.println("MyInterfaceBImpl.methodB()");
	}
}

import net.sf.cglib.proxy.Mixin;
 
public class Main {
 
	public static void main(String[] args) {
 
		Class[] interfaces =
			new Class[] { MyInterfaceA.class, MyInterfaceB.class };
		
		Object[] delegates =
			new Object[] { new MyInterfaceAImpl(), new MyInterfaceBImpl() };
			
		
		Object obj = Mixin.create(interfaces, delegates);
 
 
		MyInterfaceA myA = (MyInterfaceA)obj;
		myA.methodA();
 
 
		MyInterfaceB myB = (MyInterfaceB)obj;
		myB.methodB();
	}
}

执行结果:

MyInterfaceAImpl.methodA()
MyInterfaceBImpl.methodB()

5 Responses to “cglib 指南”

  1. On The Road » links for 2006-10-10 Says:

    […] Nirvana Studio » Blog Archive » cglib 指南 (tags: Cglib) […]

  2. On The Road » links for 2006-10-11 Says:

    […] Nirvana Studio » Blog Archive » cglib 指南 (tags: Cglib) […]

  3. 解惑 » 日志 » links for 2007-01-10 Says:

    […] cglib 指南 :: 分享知识,传播技术 (tags: java cglib 指南) […]

  4. 小叮当 » Blog Archive » 一些参考资料 Says:

    […] CLR和JRE的运行机制的初步总结 Java虚拟机 了解Java ClassLoader Java Virtual Machine Specification Java bytecode 解读字节码文件 Java Bytecode Specification and Verification ASM User Guide Hello, ASM cglig指南 Java下的框架编程–cglib的应用 AOP = Proxy Pattern + Method Reflection + Aspect DSL + 自动代码生成 深入浅出Spring AOP […]

  5. 深入了解Java ClassLoader、Bytecode 、ASM、cglib at 系统工程师天地 Says:

    […] 解读字节码文件 Java Bytecode Specification and Verification ASM User Guide Hello, ASM cglig指南 Java下的框架编程–cglib的应用 AOP = Proxy Pattern + Method Reflection + Aspect DSL + […]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>