public class CglibProxy implements MethodInterceptor { @Override public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { System.out.println("Cglib before"); Object o1 = methodProxy.invokeSuper(o, objects); System.out.println("Cglib after"); return o1; } public Object getProxy(Class clazz) {// 以下四句代码记住即可,反正就是创建代理类对象的方法,底层不要多在意 Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(clazz); enhancer.setCallback(this); return enhancer.create(); }} /** * 测试方法 */ public static void main(String[] args) {// SpringCglibProxy cglibProxy = new SpringCglibProxy();// PaySevice pay = (PaySevice) cglibProxy.getProxy(PaySevice.class);// pay.pay(1); System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "./");// CglibProxyInf cglibProxy = new CglibProxyInf();// IOrderService pay = (IOrderService) cglibProxy.getProxy(IOrderService.class);// pay.doAdd(1);// pay.getAdd(); CglibProxy cglibProxy = new CglibProxy(); PaySevice pay = (PaySevice) cglibProxy.getProxy(PaySevice.class); pay.pay(1);// System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");// OrderService orderService = new OrderService();// JdkProxy proxy = new JdkProxy(orderService);// Class<?> clazz = orderService.getClass();// IOrderService service = (IOrderService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), proxy);// service.doAdd(123456); }