Java Web框架 基础知识 静态代理和动态代理

Java Web框架 基础知识 静态代理和动态代理

一、静态代理:

1、IKeeper.java

package com.ssm.cts.pattern.proxy;

public interface IKeeper {

  public void careForBoy();
}

2、Keeper.java

package com.ssm.cts.pattern.proxy;

public class Keeper implements IKeeper {
  private String name = "";

  public Keeper(String _name) {
    this.name = _name;
  }

  public void careForBoy() {
    System.out.println(this.name + "带娃");
  }
}

3、KeeperProxy.java

package com.ssm.cts.pattern.proxy;

public class KeeperProxy implements IKeeper {
  private IKeeper keeper = null;

  public KeeperProxy(IKeeper _keeper) {
    this.keeper = _keeper;
  }

  public void careForBoy() {
    System.out.println("静态代理前置内容:买菜");
    this.keeper.careForBoy();
    System.out.println("静态代理后置内容:洗碗");
  }

}

4、Demo.java

package com.ssm.cts.pattern.proxy;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo {

  // 测试
  public static void main(String[] args) {
    // 定义一个监护人
    IKeeper keeper = new Keeper("蒋智昊");

    // 然后再定义一个保姆
    IKeeper proxy = new KeeperProxy(keeper);
  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
      
    // 开始
    System.out.println("开始时间是:" + sdf.format(new Date()));
    // 带娃
    proxy.careForBoy();
    // 结束
    System.out.println("结束时间是:" + sdf.format(new Date()) );
  }

}

二、JDK动态代理:
1、IKeeper.java

package com.ssm.cts.pattern.proxy;

public interface IKeeper {

  public void careForBoy();
}

2、Keeper.java

package com.ssm.cts.pattern.proxy;

public class Keeper implements IKeeper {
  private String name = "";

  public Keeper(String _name) {
    this.name = _name;
  }

  public void careForBoy() {
    System.out.println(this.name + "带娃");
  }
}

3、KeeperDynamicProxy.java

package com.ssm.cts.pattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class KeeperDynamicProxy implements InvocationHandler {

  private Object object;

  public KeeperDynamicProxy(Object object) {
    super();
    this.object = object;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      
    System.out.println("动态代理前置内容:买菜");
    method.invoke(object, args);
    System.out.println("动态代理后置内容:洗碗");
    
    return null;
  }

}

4、ProxyDemo.java

package com.ssm.cts.pattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class ProxyDemo {

  // 测试
  public static void main(String[] args) {
    // 定义一个监护人
    IKeeper keeper = new Keeper("蒋智昊");
    // 代理类
    InvocationHandler handler = new KeeperDynamicProxy(keeper);
    IKeeper proxy = (IKeeper) Proxy.newProxyInstance(IKeeper.class.getClassLoader(), new Class[]{IKeeper.class}, handler);
    proxy.careForBoy();
  }

}

三、Cglib动态代理:
JDK动态代理拥有局限性,那就是必须面向接口编程,没有接口就无法实现代理,我们也不可能为了代理而为每个需要实现代理的类强行添加毫无意义的接口,这时我们需要Cglib,这种依靠继承来实现动态代理的方式,不再要求我们必须要有接口。

1、Father.java

package com.ssm.cts.pattern.proxy;

public class Father {

  public Father() {
  }
  
  public void careForBoy(String name) {
    System.out.println(name + "带娃");
  }
}

2、FatherInterceptor.java

package com.ssm.cts.pattern.proxy;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class FatherInterceptor implements MethodInterceptor {

  @Override
  public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    System.out.println("动态代理前置内容:买菜");
    Object object = methodProxy.invokeSuper(o, args);
    System.out.println("动态代理后置内容:洗碗");
    return object;
  }

}

3、CglibProxyDemo.java

package com.ssm.cts.pattern.proxy;

import net.sf.cglib.proxy.Enhancer;

public class CglibProxyDemo {

  // 测试
  public static void main(String[] args) {
    // 字节码增强器
    Enhancer enchancer = new Enhancer();
    // 设置被代理类为父类
        enchancer.setSuperclass(Father.class);
        // 设置回调
        enchancer.setCallback(new FatherInterceptor());
        // 创建代理实例
        Father father = (Father)enchancer.create();
        father.careForBoy("蒋智昊");
  }

}

发表回复

您的电子邮箱地址不会被公开。