cglib动态代理相比于JDK动态代理的的优秀之处在于它是够实现非接口的动态代理,且提供了多种回调方式,这里就将callbackFilter和MethodInterceptor两种
被代理类
package com.yangpeng.proxy; public class BookPorxyImpl { public void addBook() { System.out.println("增加图书的普通放方法"); } public void addBall() { System.out.println("增加Ball的普通放方法"); } }
提供统一的回调方式,cglib的主要回调是MethodInterceptor接口,和用于创建动态代理的Enhancer类
package com.yangpeng.proxy; 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 BookProxyLib implements MethodInterceptor{ private Object target;
//这一个方法你也可以不写在这里,这里只是采用Enhancer对传入的原对象做一个包装,返回一个代理类 public Object getInstance(Object target) { this.target = target; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(this.target.getClass()); enhancer.setCallback(this); return enhancer.create(); } //接口实现 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("事务开始"); proxy.invokeSuper(obj, args); System.out.println("事务结束"); return null; } }
另外一种回调:
package com.yangpeng.proxy; import java.lang.reflect.Method; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; public class BookProxyLib2 implements MethodInterceptor{ public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("不错哦!"); return null; } }
CallbackFilter:
package com.yangpeng.proxy; import java.lang.reflect.Method; import com.sun.xml.internal.ws.api.message.AttachmentEx.MimeHeader; import net.sf.cglib.proxy.CallbackFilter; public class BookCallBackFilter implements CallbackFilter { /** * 方法返回的值时和callback回调接口返回的数组一一对应的数组下标 */ public int accept(Method method) { String name =method.getName(); if("addBook".equals(name)) { return 0; } return 1; } }
测试:
import com.sun.org.apache.bcel.internal.generic.NEW; import net.sf.cglib.proxy.Callback; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.NoOp; public class TestLib { public static void main(String[] args) { proxy(); callbackProxy(); }
public static void proxy() { System.out.println("---------------统一采用MethodInterceptor,所有的业务都经过同一个intercept调用---------"); BookProxyLib cglib = new BookProxyLib(); BookPorxyImpl bookcglib = (BookPorxyImpl) cglib.getInstance(new BookPorxyImpl()); bookcglib.addBook(); bookcglib.addBall(); System.out.println("-------------------------------------------------------------------------------"); }
//callback Proxy public static void callbackProxy() { System.out.println("----------------采用callbackFilter对不同方法实现不同代理逻辑------------------------------"); BookPorxyImpl bookImpl = new BookPorxyImpl(); Enhancer enhancer = new Enhancer(); // 设置代理目标 enhancer.setSuperclass(bookImpl.getClass());
//这里声明一个Callback数组,此处对bookProxyImpl的两个方法分别做使用了两个回调, Callback[] callbacks = new Callback[] {new BookProxyLib(),new BookProxyLib2(),NoOp.INSTANCE }; enhancer.setCallbacks(callbacks); enhancer.setCallbackFilter(new BookCallBackFilter()); BookPorxyImpl impl = (BookPorxyImpl) enhancer.create(); impl.addBall(); impl.addBook(); } }