介绍
Spring动态代理可以选择使用jdk动态代理, 或者cglib动态代理, cglib动态代理位于 net.sf.cglib.proxy 包下.
使用时涉及
接口: net.sf.cglib.proxy.MethodInterceptor
用来生成动态子类的类类: net.sf.cglib.proxy.Enhancer
注意: cglib 动态代理是基于类的代理, 是通过对指定的业务类生成一个子类, 并覆盖其中业务方法实现代理. 因为使用继承, 所以被代理类不能使 final 修饰
使用步骤
1.创建MethodInterceptor接口的实现类, 并编写intercept方法的实现
2.通过methodProxy.invokeSuper(o, objects);调用父类的方法
3.创建Enhancer, 通过 setSuperclass(Class superclass)方法指定父类(被代理类), 通过 setCallback(final Callback callback)方法指定代理
4.enhancer.create() 生成代理, 调用被代理类的方法
代码演示
创建MethodInterceptor接口的实现类
/*** 基于类的代理 即使类没有实现接口也可以被代理* 主要是基于类生成一个继承的子类 所以 类和方法不要声明为 final*** @author liuzhihang* @date 2018/5/18 10:10*/public class MyMethodInterceptor implements MethodInterceptor {@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("cglib动态代理 before . . .");Object invoke = null;try {invoke = methodProxy.invokeSuper(o, objects);} catch (Throwable throwable) {throwable.printStackTrace();System.err.println("cglib动态代理 error: " + throwable.getMessage());} finally {System.out.println("cglib动态代理 after . . .");}return invoke;}}
创建Enhancer
创建Enhancer, 通过 setSuperclass(Class superclass)方法指定父类(被代理类), 通过 setCallback(final Callback callback)方法指定代理
public class CglibMainTest {public static void main(String[] args) {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(SubjectCglib.class);enhancer.setCallback(new MyMethodInterceptor());SubjectCglib subjectCglib = (SubjectCglib) enhancer.create();System.err.println(subjectCglib.getAge("liuzhihang"));}}
可以将二者合并到MyInterceptor中
/*** 基于类的代理 即使类没有实现接口也可以被代理*** @author liuzhihang* @date 2018/5/18 10:10*/public class MyCglibInterceptor implements MethodInterceptor {private Object object;public Object getInstance(Object object) {this.object = object;Enhancer enhancer = new Enhancer();enhancer.setSuperclass(object.getClass());enhancer.setCallback(this);return enhancer.create();}@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println("cglib动态代理 before . . .");Object invoke = null;try {invoke = methodProxy.invokeSuper(o, objects);} catch (Throwable throwable) {throwable.printStackTrace();System.err.println("cglib动态代理 error: " + throwable.getMessage());} finally {System.out.println("cglib动态代理 after . . .");}return invoke;}}
