代理模式由三部分组成:委托类,代理类,抽象类。

委托类、代理类都实现了抽象类。

静态代理:点到点

 

//抽象角色
public interface ProxyTestDao {

    void add();
    
}

 

首先,(ProxyTestDaoImpl  实现抽象类)

public class ProxyTestDaoImpl implements ProxyTestDao{

    public void add() {
        System.out.println("ProxyTestDaoImpl.add();");
    }

}

(ProxyTestDaoProxyImpl 实现抽象类)

public class ProxyTestDaoProxyImpl implements ProxyTestDao{
    //真实角色
    private ProxyTestDao proxyTestDao;
    //代理角色的构造器
    public ProxyTestDaoProxyImpl(ProxyTestDao proxyTestDao) {
        this.proxyTestDao = proxyTestDao;
    }
    //代理角色的方法,可以附加自己的操作
    public void add() {
        System.out.println("前置代理");
        proxyTestDao.add();
        System.out.println("后置代理");
    }

这样准备工作已完成,只需要委托类委托代理类实现具体方法。

 

 1 public class ProxyTestAction {
 2 
 3     public static void main(String[] args) {
 4         //委托类(
 5         ProxyTestDaoImpl ptd = new ProxyTestDaoImpl();
 6         //代理类
 7         ProxyTestDaoProxyImpl ptdp = new ProxyTestDaoProxyImpl(ptd);
 8         //代理类的方法(附加了自己的操作)
 9         ptdp.add();
10     }
11 
12 }

动态代理:面到面

JDK Proxy:需要实现java.lang包下的InvocationHandler类

public class LogHandler implements InvocationHandler{
    
    private Object obj;
    
    public LogHandler(Object obj) {
        this.obj = obj;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        
        System.out.println("执行前...");
        method.invoke(obj, args);
        System.out.println("执行后...");
        
        return null;
    }
    

}

通过实现java.lang.reflect.InvocationHandler,InvocationHandler类中的invoke方法,第一个参数proxy代表的是代理类、第二个参数method为当前执行的方法,第三个参数为接口数组。

public class ProxyTestAction {

    public static void main(String[] args) {
        //委托类(真实角色)
        ProxyTestDao ptd = (ProxyTestDao) Proxy.newProxyInstance(
                    ProxyTestDao.class.getClassLoader(),
                    ProxyTestDaoImpl.class.getInterfaces(),
                    new  LogHandler(new ProxyTestDaoImpl()));
        ptd.add();
        
    }

}

Cglib : 实现MethodInterceptor类

package api.cloudp.cc.cloudpserver.web.interceptor;


public class ProxyTestDao {

    void add(){
        System.out.println("ProxyTestDao.add();");
    };
    
}

 

public class LogHandler  implements MethodInterceptor {

    
    public Object intercept(Object arg0, Method method, Object[] arg2,
            MethodProxy methodProxy) throws Throwable {
        System.out.println("日志记录");
        return methodProxy.invokeSuper(arg0, arg2);
    }

}
public class ProxyTestAction {

public  static  ProxyTestDao  getDao(){
        
        Enhancer  enhancer=new Enhancer();
            enhancer.setSuperclass(ProxyTestDao.class);   // 不需要new 委托类。  
            enhancer.setCallback(new LogHandler());   // 代理类的执行内容
            return (ProxyTestDao) enhancer.create();  // 返回一个代理对象。
    }

    public static void main(String[] args) {
        //委托类(真实角色)
        ProxyTestDao ptd = ProxyTestAction.getDao();
        System.out.println(ptd.getClass().getName());
        ptd.add();
    }

}