CGLIB代理类,用CGLIB来实现一个代理类。大致原理描述如下:
1、使用Enhancer类来生成一个继续于被代理类的子类,此子类会重写被代理类(父类)中所有的非final的public方法;
2、当调用代理类的某个方法时实际上通过一个MethodInterceptor委托来间接调用的;
3、MethodInterceptor的intercept方法会对被代理类的方法进行增强(增强的逻辑是通过Interceptor接口来实现的)并调用被代理类(super class)的相关方法。
调用过程如下图:
CglibProxy类实现核心代理逻辑,并提供一个Interceptor接口供客户端来实现增强逻辑。
package proxy; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; /** * @author xfyou */ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class CglibProxy<T> { private final Class<T> targetClass; private final MethodInterceptor methodInterceptor; public static <T> CglibProxy<T> newInstance(Class<T> targetClass, Interceptor interceptor) { return new CglibProxy<>(targetClass, new MethodInterceptor() { @Override public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { interceptor.before(obj); Object invokeResult; try { invokeResult = proxy.invokeSuper(obj, args); } catch (Throwable e) { interceptor.exception(obj, e); throw new InvocationTargetException(e); } interceptor.after(obj); return invokeResult; } }); } @SuppressWarnings("unchecked") public T createNewObj() { return (T) createNewEnhancer().create(); } private Enhancer createNewEnhancer() { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(targetClass); enhancer.setCallback(methodInterceptor); return enhancer; } public interface Interceptor { /** * Do something before invoke the super class method * * @param obj Instances of proxied objects */ void before(Object obj); /** * Do something after invoked the super class method * * @param obj Instances of proxied objects */ void after(Object obj); /** * Do something when exception occurred when invoke the super class method * * @param obj Instances of proxied objects * @param e Exception */ void exception(Object obj, Throwable e); } }
WorkProxy类用来对Work类进行代理,生成的是一个单例的代理类。
package proxy; import proxy.CglibProxy.Interceptor; /** * @author xfyou * @date 2019/10/15 */ public class WorkProxy { private static Work proxyObject; private static Interceptor interceptor = new Interceptor() { @Override public void before(Object obj) { System.out.println("before"); } @Override public void after(Object obj) { System.out.println("after"); } @Override public void exception(Object obj, Throwable e) { System.out.println("exception"); } }; static { proxyObject = CglibProxy.newInstance(Work.class, interceptor).createNewObj(); } public static Work get() { return proxyObject; } }
被代理的Work类如下:
package proxy; /** * Work */ public class Work { public void doWork() { System.out.println("Do work"); } }
测试代码:
package proxy; /** * @author xfyou * @date 2019/10/15 */ public class Test { public static void main(String[] args) { WorkProxy.get().doWork(); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20621.html