静态代理->装饰器模式
jdk动态代理
cglib
/*** CGLIB实现动态代理不需要接口,底层是asm*/public class Main {public static void main(String[] args) {Enhancer enhancer = new Enhancer();enhancer.setSuperclass(Tank.class);enhancer.setCallback(new TimeMethodInterceptor());Tank tank = (Tank)enhancer.create();tank.move();}}class TimeMethodInterceptor implements MethodInterceptor {@Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {System.out.println(o.getClass().getSuperclass().getName());System.out.println("before");Object result = null;result = methodProxy.invokeSuper(o, objects);System.out.println("after");return result;}}class Tank {public void move() {System.out.println("Tank moving claclacla...");try {Thread.sleep(new Random().nextInt(10000));} catch (InterruptedException e) {e.printStackTrace();}}}
asm
修改class二进制码
