标签:private lang targe book code ack ddb ... inter
package proxy.cglib; 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 BookFacadeCglib implements MethodInterceptor { private Object target;//业务类对象,供代理方法中进行真正的业务方法调用 //相当于JDK动态代理中的绑定 public Object getInstance(Object target) { this.target = target; //给业务对象赋值 Enhancer enhancer = new Enhancer(); //创建加强器,用来创建动态代理类 enhancer.setSuperclass(this.target.getClass()); //为加强器指定要代理的业务类(即:为下面生成的代理类指定父类) //设置回调:对于代理类上所有方法的调用,都会调用CallBack,而Callback则需要实现intercept()方法进行拦 enhancer.setCallback(this); // 创建动态代理类对象并返回 return enhancer.create(); } // 实现回调方法 public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { System.out.println("预处理——————"); proxy.invokeSuper(obj, args); //调用业务类(父类中)的方法 System.out.println("调用后操作——————"); return null; } }
package proxy.cglib; public class BookFacadeImpl1 { public void addBook() { System.out.println("新增图书..."); } }
package proxy.cglib; public class Test { public static void main(String[] args) { BookFacadeCglib proxy=new BookFacadeCglib(); BookFacadeImpl1 bfi = (BookFacadeImpl1) proxy.getInstance(new BookFacadeImpl1()); bfi.addBook(); } }
标签:private lang targe book code ack ddb ... inter
原文地址:https://www.cnblogs.com/Danial7777777/p/9398622.html