坦克大战(一期)-设计模式-Cglib动态代理

DesignPatterns
bingor 2 years ago
parent 6b171063a4
commit 2cfe9813bd

@ -0,0 +1,28 @@
package com.msb.proxy.cglib;/**
* @Author bingor
* @Date 2022/10/17 14:16
* @Description: com.msb.proxy.cglib
* @Version: 1.0
*/
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
*@ClassName LogMethodInterceptor
*@Description TODO
*@Author bingor
*@Date 2022/10/17 14:16
*@Version 3.0
*/
public class LogMethodInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("method execute start...");
Object result = methodProxy.invokeSuper(o, objects);
System.out.println("method execute end...");
return result;
}
}

@ -0,0 +1,36 @@
package com.msb.proxy.cglib;/**
* @Author bingor
* @Date 2022/10/17 9:41
* @Description: com.msb.proxy.v5
* @Version: 1.0
*/
import org.springframework.cglib.proxy.Enhancer;
import java.util.Random;
/**
*@ClassName Tank
*@Description TODO
*@Author bingor
*@Date 2022/10/17 9:41
*@Version 3.0
*/
public class Tank {
public void move() {
System.out.println("tank move……");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(Tank.class);
enhancer.setCallback(new LogMethodInterceptor());
Tank tank = (Tank) enhancer.create();
tank.move();
}
}

@ -1,4 +1,4 @@
package com.msb.proxy.v8;/** package com.msb.proxy.jdk;/**
* @Author bingor * @Author bingor
* @Date 2022/10/17 10:30 * @Date 2022/10/17 10:30
* @Description: com.msb.proxy.v8 * @Description: com.msb.proxy.v8

@ -1,4 +1,4 @@
package com.msb.proxy.v8; package com.msb.proxy.jdk;
/** /**
* @Author bingor * @Author bingor

@ -1,4 +1,4 @@
package com.msb.proxy.v8;/** package com.msb.proxy.jdk;/**
* @Author bingor * @Author bingor
* @Date 2022/10/17 9:41 * @Date 2022/10/17 9:41
* @Description: com.msb.proxy.v5 * @Description: com.msb.proxy.v5
Loading…
Cancel
Save