cglib 指南

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()

你可能感兴趣的:(cglib)