package 动态代理;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy;
//cglib动态代理 public class CglibProxy implements MethodInterceptor{
private Object target; /** * 创建代理对象 * targer * */ public Object getInstance(Object target){ //System.out.println(this.target.getClass()); this.target = target; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(this.target.getClass()); //回调方法 enhancer.setCallback(this); //创建代理对象 return enhancer.create(); } //回调方法 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("事物开始:类"+obj.getClass()+" 方法名:"+method.getName()); proxy.invokeSuper(obj, args); System.out.println("事物结束"); return null; } }