import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class Test{
public static void main(String[] args) {
MyProxy proxy = new MyProxy();
Biz biz = (Biz)proxy.getProxy(Biz.class);
biz.biz();
}
}
class MyProxy implements MethodInterceptor {
private Enhancer enhancer = new Enhancer();
public Object getProxy(Class clazz) {
enhancer.setSuperclass(clazz);
enhancer.setCallback(this);
return enhancer.create();
}
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
System.out.println("Before......");
Object result = proxy.invokeSuper(obj, args);
System.out.println("After......");
return result;
}
}
class Biz {
public void biz() {
System.out.println("biz()......");
}
}
Cglib动态代理实例
最新推荐文章于 2024-04-17 16:08:00 发布