ITeye博客

JAVA 动态dialing

udvs · 2009-06-17 14:57 · JAVA 基础 · 编程语言 · Markdown

看了1月的程序员杂志,看到动态代理的讲述,写下如下学习笔记,其中demo做了改动,更好体现动态代理的概念。

一:动态代理实践

1.接口类:Foo.java

package dymaticproxy;

/**

  • @author zhaolh

  • @version 1.0

*/

public interface Foo {

void doAction();

}

2.接口的实现1

package dymaticproxy;

/**

  • @author zhaolh

  • @version 1.0

*/

public class FooImpl implements Foo {

public FooImpl() {

}

public void doAction() {

    /**@todo Implement this dymaticproxy.Foo method*/

    //throw new java.lang.UnsupportedOperationException("Method doAction() not yet implemented.");

    System.out.println("in FooImp1.doAction()");

}

}

3.接口的实现2

package dymaticproxy;

/**

  • @author zhaolh

  • @version 1.0

*/

public class FooImpl2 implements Foo {

public FooImpl2() {

}

public void doAction() {

    /**@todo Implement this dymaticproxy.Foo method*/

    //throw new java.lang.UnsupportedOperationException("Method doAction() not yet implemented.");

    System.out.println("in FooImp2.doAction()");

}

}

4.处理器类

package dymaticproxy;

import java.lang.reflect.Method;

import java.lang.reflect.InvocationHandler;

/**

  • @author zhaolh

  • @version 1.0

*/

public class CommonInvocationHandler implements InvocationHandler {

//动态执行对象,需要回调的对象

private Object target;



//支持构造子注射

public CommonInvocationHandler() {



}



//支持构造子注射

public CommonInvocationHandler(Object target) {

  setTarget(target);

}



/**

 * 采用setter方法注射

 * @param target

 */

public void setTarget(Object target)

{

  this.target=target;

}



/**

 * 调用proxy中指定的方法method,并传入参数列表args

 * @param proxy 代理类的类型,例如定义对应method的代理接口

 * @param method 被代理的方法

 * @param args  调用被代理方法的参数

 * @return

 * @throws java.lang.Throwable

 */

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

    /**@todo Implement this java.lang.reflect.InvocationHandler method*/

    //throw new java.lang.UnsupportedOperationException("Method invoke() not yet implemented.");

    return method.invoke(target,args);

}

}

5.动态代理示范程序

package dymaticproxy;

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Proxy;

/**

  • @author zhaolh

  • @version 1.0

*/

public class Demo {

public static void main(String args) {

    //1.通用的动态代理实现

    CommonInvocationHandler handler = new CommonInvocationHandler();

    Foo f;



    //2.接口实现1

    handler.setTarget(new FooImpl());

    //方法参数说明:代理类、代理类实现的接口列表、代理类的处理器

    //关联代理类、代理类中接口方法、处理器,但代理类中接口方法被调用时,会自动分发到处理器的invoke方法

    //如果代理类没有实现指定接口列表,会抛出非法参数异常

    f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),

                                              new Class { Foo.class },

                                              handler);

    f.doAction();



    //3.接口实现2

    handler.setTarget(new FooImpl2());

    f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),

                                      new Class { Foo.class },

                                      handler);

    f.doAction();

 }

}

二:动态代理的总结

代理类(可以单个接口和类)、代理类接口(可以是多个接口)、处理器构成一种动态的关系,创建和运行期都可以随意改变的。

例如上例中Proxy.newProxyInstance创建的(Foo.class.getClassLoader()类型)实例f,可以实现new Class { Foo.class }中指定接口,并和handler关联。所有通过f的调用接口(new Class { Foo.class })中方法(doAction())都被转发到invoke()中动态调用target的doAction()实现。

总之动态代理提供一种动态代理类,它的实例可以实现任意的业务接口,并且可以在运行时决定某个实例实现那个接口。同时可以实现对特有调用方法的拦截。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。