JDK动态代理

  jdk动态代理只能代理接口,因为java的单继承动态代理的代理类继承了Proxy类,就不能在继承其他的类,所以只能靠接口实现被代理类的接口。

  案例

  public interface UserManager {

  void addUser(Integer id,String userName);

  void delUser(Integer id);

  void findUser(Integer id);

  void modifyUser(Integer id,String userName);

  }

  public class UserManagerImpl implements UserManager {

  @Override

  public void addUser(Integer id,String userName) {

  System.out.println("userManager.addUser()");

  }

  @Override

  public void delUser(Integer id) {

  System.out.println("userManager.delUser()");

  }

  @Override

  public void findUser(Integer id) {

  System.out.println("userManager.findUser()");

  }

  @Override

  public void modifyUser(Integer id,String userName) {

  System.out.println("userManager.modifyUser()");

  }

  }

  public class LogHandler implements InvocationHandler{

  //目标对象

  private Object targetObject;

  public Object newProxyInstance(Object targetObject) {

  this.targetObject=targetObject;

  /*

  * param1那个类加载器去加载代理对象

  * param2 需要实现的接口

  * param3动态代理方法在执行时,会调用invoke方法去执行

  */

  return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),targetObject.getClass().getInterfaces(), this);

  }

  @Override

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

  //打印目标方法参数

  for (int i = 0; i < args.length; i++) {

  System.out.println(args[i]);

  }

  Object ret=null;

  try {

  System.out.println("start");//执行目标方法前

  ret=method.invoke(targetObject, args);

  System.out.println("success");//执行目标方法后

  } catch (Exception e) {

  e.printStackTrace();

  System.out.println("error");

  throw e;

  }

  return ret;

  }

  }

  Cglib动态代理

  Cglib就是子类继承指定类然后重写类方法,所以最好不要在指定类上加final修饰符

  案例

  Cglib需要依赖包

  cglib

  cglib

  2.2.2

  //Cglib动态代理实现MethodInterceptor接口

  public class CglibProxy implements MethodInterceptor{

  private Object target;

  @Override郑州人流专科医院 http://www.03912316666.com/

  public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {

  System.out.println("start");

  //输出目标方法参数

  for (int i = 0; i < args.length; i++) {

  System.out.println(args[i]);

  }

  Object invoke=method.invoke(target, args);

  System.out.println("end");

  return invoke;

  }

  public Object getCglibProxy(Object targetObject) {

  this.target=targetObject;

  Enhancer enhancer=new Enhancer();

  //因为Cglig需要生成一个子类,所以需要指定一个父类

  enhancer.setSuperclass(targetObject.getClass());

  enhancer.setCallback(this);

  Object result=enhancer.create();//创建并返回代理对象

  return result;

  }

  public static void main(String[] args) {

  CglibProxy cglibProxy=new CglibProxy();

  UserManagerImpl userManager=(UserManagerImpl)cglibProxy.getCglibProxy(new UserManagerImpl());

  userManager.addUser(666 "6666");

  }

  }

  //并没有实现父接口

  public class UserManagerImpl {

  public void addUser(Integer id,String userName) {

  System.out.println("userManager.addUser()");

  }

  public void delUser(Integer id) {

  System.out.println("userManager.delUser()");

  }

  public void findUser(Integer id) {

  System.out.println("userManager.findUser()");

  }

  public void modifyUser(Integer id,String userName) {

  System.out.println("userManager.modifyUser()");

  }

  }