动态代理3之代理工厂实现

动态代理的深化:承接动态代理2( 对 上级实例进行 深化),也就是 spring 的前身

(必须掌握)实例:

文件说明:1.有一个Waiter接口,该接口有一个 server() 、ShouQian()方法

2.有一个 Waiter接口的实现类,ManWaiter类

3.一个 测试Test类

     4. 后置增强接口: AfterAdvice 接口

     5.前置增强接口:BeforeAdvice接口

     6. 生成代理对象 的 ProxyFactory类

1. Waiter.java 接口

// 服务员

public interface Waiter {

    // 服务

    public void serve();

    

    //收钱

    public void shouQian();

}

 

2. BeforeAdvice.java 接口,前置增强接口

/**

* 前置增强

* @author cxf

*

*/

public interface BeforeAdvice {

    public void before();

}

 

3. AfterAdvice.java 接口,后置增强接口

/**

* @function 后置增强

* @author not-bug

*

*/

public interface AfterAdvice {

    public void after();

}

 

4 . ManWaiter.java 类,该类实现了 Waiter接口

//该类实现了 Waiter 接口

public class ManWaiter implements Waiter {

    public void serve() {

        System.out.println("服务中...");

    }

    

    public void shouQian() {

        System.out.println("混蛋,给我钱!");

    }

}

 

5. ProxyFactory.java 类,用来生成代理对象的类

/**

* 它用来生成代理对象

* 它需要所有的参数

* * 目标对象

* * 增强

* @author cxf

*/

/**

* 1. 创建代理工厂

* 2. 给工厂设置三样东西:

* * 目标对象:setTargetObject(xxx);

* * 前置增强:setBeforeAdvice(该接口的实现)

* * 后置增强:setAfterAdvice(该接口的实现)

* 3. 调用createProxy()得到代理对象

* * 执行代理对象方法时:

* > 执行BeforeAdvicebefore()

* > 目标对象的目标方法

* > 执行AfterAdviceafter()

* @author cxf

*

*/

public class ProxyFactory {

    private Object targetObject;//目标对象

    private BeforeAdvice beforeAdvice;//前置增强

    private AfterAdvice afterAdvice;//后置增强

      

    

    /**

     * 用来生成代理对象

     * @return

     */

    public Object createProxy() {

        /*

         * 1. 给出三大参数

         */

        ClassLoader loader = this.getClass().getClassLoader();

        Class[] interfaces = targetObject.getClass().getInterfaces();

        InvocationHandler h = new InvocationHandler() {

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

                    throws Throwable {

                /*

                 * 在调用代理对象的方法时会执行这里的内容

                 */

                // 执行前置增强

                if(beforeAdvice != null) {

                    beforeAdvice.before();

                }

                

                Object result = method.invoke(targetObject, args);//执行目标对象的目标方法

                // 执行后置增强

                if(afterAdvice != null) {

                    afterAdvice.after();

                }

                

                // 返回目标对象的返回值

                return result;

            }

        };

        /*

         * 2. 得到代理对象

         */

        Object proxyObject = Proxy.newProxyInstance(loader, interfaces, h);

        return proxyObject;

    }

      

    

    public Object getTargetObject() {

        return targetObject;

    }

    public void setTargetObject(Object targetObject) {

        this.targetObject = targetObject;

    }

    public BeforeAdvice getBeforeAdvice() {

        return beforeAdvice;

    }

    public void setBeforeAdvice(BeforeAdvice beforeAdvice) {

        this.beforeAdvice = beforeAdvice;

    }

    public AfterAdvice getAfterAdvice() {

        return afterAdvice;

    }

    public void setAfterAdvice(AfterAdvice afterAdvice) {

        this.afterAdvice = afterAdvice;

    }

}

 

6. Test.java ,测试类

 

/*

* 目标是让目标对象和增强都可以切换!

*/

public class Demo3 {

    @Test

    public void fun1() {

        ProxyFactory factory = new ProxyFactory();//创建工厂

        factory.setTargetObject(new ManWaiter());//设置目标对象

        factory.setBeforeAdvice(new BeforeAdvice() {//设置前置增强

            public void before() {

                System.out.println("您好不好!");

            }

        });

        

        factory.setAfterAdvice(new AfterAdvice() {//设置后置增强

            public void after() {

                System.out.println("再见不见!");

            }

        });

        

        Waiter waiter = (Waiter)factory.createProxy();

        //waiter.shouQian();

        waiter.serve();

    }

    

    //转账方法

    public void zhuanZhang() {

        /*

         * 1.

         * 2.

         * 3.

         */

    }

}

 

运行结果:

posted @ 2016-05-02 20:03  runningto  阅读(664)  评论(0编辑  收藏  举报