使用CGLIB生成代理
CGLIB可以生成目标类的子类,并重写父类非final修饰符的方法。
//实现methid public class CGLIBProxy2 implements MethodInterceptor { // 目标类对象 private Object targetObject; // 根据目标类对象创建代理子类 public Object createProxyObject(Object obj) { targetObject = obj; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(obj.getClass()); enhancer.setCallback(this); return enhancer.create(); } @Override public Object intercept(Object arg0, Method arg1, Object[] arg2, MethodProxy arg3) throws Throwable { System.out.println("1"); Object o = arg3.invoke(targetObject, arg2); System.out.println("2"); return o; } }